digitalmars.D.learn - Is there any performance penalty for static if?
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (21/21) May 15 2019 Hi,
- Jonathan M Davis (17/38) May 15 2019 If you really want to see what happens (for any piece of code), then you...
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (2/11) May 15 2019 Thanks a lot! This is what I was exactly expecting.
- user1234 (6/27) May 15 2019 You've been given the answer but about this particular piece of
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (2/10) May 16 2019 Yes, it is now much better. Thank you.
Hi, Maybe I already know the answer, but have to be sure about this. I am emulating this cpp code "int val = mat.at<int>(row, col)" like: T at(T)(int row, int col){ static if (T.stringof == "float"){ return getFloatAt(row, col); } else static if (T.stringof == "double"){ return getDoubleAt(row, col); } else static if (T.stringof == "int"){ return getIntAt(row, col); } else static if (T.stringof == "ubyte"){ return getUCharAt(row, col); } else static if (T.stringof == "byte"){ return getSCharAt(row, col); } else static if (T.stringof == "short"){ return getShortAt(row, col); } } This works as expected, and I know conditions of "static if" is determined at compile time and I assume no speed penalty here?
May 15 2019
On Wednesday, May 15, 2019 4:03:39 PM MDT Ferhat Kurtulmuş via Digitalmars- d-learn wrote:Hi, Maybe I already know the answer, but have to be sure about this. I am emulating this cpp code "int val = mat.at<int>(row, col)" like: T at(T)(int row, int col){ static if (T.stringof == "float"){ return getFloatAt(row, col); } else static if (T.stringof == "double"){ return getDoubleAt(row, col); } else static if (T.stringof == "int"){ return getIntAt(row, col); } else static if (T.stringof == "ubyte"){ return getUCharAt(row, col); } else static if (T.stringof == "byte"){ return getSCharAt(row, col); } else static if (T.stringof == "short"){ return getShortAt(row, col); } } This works as expected, and I know conditions of "static if" is determined at compile time and I assume no speed penalty here?If you really want to see what happens (for any piece of code), then you can look at the generated assembly, but static if is completely a compile-time construct. It affects which code ends up in the binary. For instance, if your at function there were instantiated with double, then the resulting function would be T at(int row, int col) { return getDoubleAt(row, col); } None of the rest of the code would be there. static if is just a tool for controlling code generation. It _does_ increase compile times (albeit not much), because it has to be processed at compile time, but it does not exist in any way shape or form at runtime and thus can only affect runtime by affecting what code gets compiled in. - Jonathan M Davis
May 15 2019
On Wednesday, 15 May 2019 at 22:13:18 UTC, Jonathan M Davis wrote:On Wednesday, May 15, 2019 4:03:39 PM MDT Ferhat Kurtulmuş via Digitalmars- d-learn wrote:Thanks a lot! This is what I was exactly expecting.[...]If you really want to see what happens (for any piece of code), then you can look at the generated assembly, but static if is completely a compile-time construct. It affects which code ends up in the binary. For instance, if your at function there were instantiated with double, then the resulting function would be [...]
May 15 2019
On Wednesday, 15 May 2019 at 22:03:39 UTC, Ferhat Kurtulmuş wrote:Hi, Maybe I already know the answer, but have to be sure about this. I am emulating this cpp code "int val = mat.at<int>(row, col)" like: T at(T)(int row, int col){ static if (T.stringof == "float"){ return getFloatAt(row, col); } else static if (T.stringof == "double"){ return getDoubleAt(row, col); } else static if (T.stringof == "int"){ return getIntAt(row, col); } else static if (T.stringof == "ubyte"){ return getUCharAt(row, col); } else static if (T.stringof == "byte"){ return getSCharAt(row, col); } else static if (T.stringof == "short"){ return getShortAt(row, col); } } This works as expected, and I know conditions of "static if" is determined at compile time and I assume no speed penalty here?You've been given the answer but about this particular piece of code, rather use the "is" expression static if (is(T == float)) {} else static if (is(T == double)) {} etc.
May 15 2019
On Thursday, 16 May 2019 at 00:18:25 UTC, user1234 wrote:On Wednesday, 15 May 2019 at 22:03:39 UTC, Ferhat Kurtulmuş wrote:Yes, it is now much better. Thank you.[...]You've been given the answer but about this particular piece of code, rather use the "is" expression static if (is(T == float)) {} else static if (is(T == double)) {} etc.
May 16 2019