www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there any performance penalty for static if?

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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:
 [...]
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 [...]
Thanks a lot! This is what I was exactly expecting.
May 15 2019
prev sibling parent reply user1234 <user1234 12.de> writes:
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
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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:
 [...]
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.
Yes, it is now much better. Thank you.
May 16 2019