www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Question about Template

reply "Michael" <pongad gmail.com> writes:
Hello!
   I have a little question about templates. I am trying to write 
a template "timer" that takes a function and times how long the 
function takes to execute. Something like
   auto time = timer!sort(array);
is equivalent to
   auto start = Clock.currStdTime();
   sort(array);
   auto time = Clock.currStdTime() - time;

except of course that the variable "start" is not created in the 
former case.

The closest I came to the correct template is
template(alias F) timer {
   auto timer {
     auto start = Clock.currStdTime();
     F();
     return Clock.currStdTime() - time;
   }
}

The problem with the template is I cannot pass any argument into 
"F". How should I fix this problem?
Also, I am not entirely sure why I need to write "alias F" 
instead of just "F", any explanation would be appreciated.

Thank you guys,
Michael
Jun 23 2012
next sibling parent simendsjo <simendsjo gmail.com> writes:
On Sat, 23 Jun 2012 17:30:01 +0200, Michael <pongad gmail.com> wrote:

 Hello!
    I have a little question about templates. I am trying to write a  
 template "timer" that takes a function and times how long the function  
 takes to execute. Something like
    auto time = timer!sort(array);
 is equivalent to
    auto start = Clock.currStdTime();
    sort(array);
    auto time = Clock.currStdTime() - time;

 except of course that the variable "start" is not created in the former  
 case.

 The closest I came to the correct template is
 template(alias F) timer {
    auto timer {
      auto start = Clock.currStdTime();
      F();
      return Clock.currStdTime() - time;
    }
 }

 The problem with the template is I cannot pass any argument into "F".  
 How should I fix this problem?
 Also, I am not entirely sure why I need to write "alias F" instead of  
 just "F", any explanation would be appreciated.

 Thank you guys,
 Michael

import std.datetime; import std.traits; import std.stdio; struct TimerResult(T) { TickDuration duration; T result; } auto timer(alias fn, Args...)(Args args) { auto sw = StopWatch(AutoStart.yes); auto res = fn(args); return TimerResult!(typeof(res))(sw.peek, res); } T f(T)(T[] arr) { import core.thread; Thread.sleep(dur!"seconds"(1)); return arr[0]; } void main() { auto res = timer!f([1,2,3,4]); writeln("first elem: ", res.result); writeln("time used: ", res.duration.msecs); }
Jun 23 2012
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/23/2012 08:30 AM, Michael wrote:

 Also, I am not entirely sure why I need to write "alias F" instead of
 just "F", any explanation would be appreciated.

F alone would mean a 'type', for example to be used in the template as F var; Ali
Jun 23 2012
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--20cf300fafe5515daf04c33244da
Content-Type: text/plain; charset=UTF-8

 The closest I came to the correct template is
 template(alias F) timer {
  auto timer {
    auto start = Clock.currStdTime();
    F();
    return Clock.currStdTime() - time;
  }
 }

 The problem with the template is I cannot pass any argument into "F". How

As Simendsjo said, you can use a function template to get what you want and a nicer syntax as the same time. Slightly different code can be obtained by starting from the above code: template timer(alias F) // capture F's name { auto timer(Args...)(Args args) // there are your parameters { import std.datetime; // inner imports auto start = Clock.currStdTime(); F(args); return Clock.currStdTime() - start; } } So it's a template that gets expanded into another template with the same name, a function. Note that the construct: template (T...) { T foo(T t) // some function that depends on the template parameters { } } can get syntax sugar like this: T foo(T...)(T t) { } you can 'fuse' the external template and the inner code to get a function template. The same for classes and structs. Now, for something a bit more hairy: Simendsjo's version and mine are not strictly equivalent: - his version is simpler to type and to reason about. You should use it, it's the D way to do this kind of thing. - mine (the two level templates) gets an interesting effect: I can use the first level only (getting the function name), keeping the second level for later: // mine alias timer!foo tfoo; // tfoo is the inner template, the function template /* a bit later */ tfoo(args1); tfoo(args2); import std.algorithm; Args[] argsArray; auto m = map!(tfoo)(argsArray); // applying tfoo on all arguments groups // Simendsjo: timer!foo(args1); timer!foo(args2); // You must provide 'foo' for each call.
 Also, I am not entirely sure why I need to write "alias F" instead of

Because in your case, you want to get the name, the symbol. So use an alias parameter. 'F' would be to deal with types, as Ali said. For example: auto foo(F, Args...)(F fn, Args args) { ... } in this case, fn is passed as a runtime value of type F: timer(&foo, args); // the compiler automatically deduces the types of fn and args. Philippe --20cf300fafe5515daf04c33244da Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p>&gt; The closest I came to the correct template is<br> &gt; template(alias F) timer {<br> &gt; =C2=A0auto timer {<br> &gt; =C2=A0 =C2=A0auto start =3D Clock.currStdTime();<br> &gt; =C2=A0 =C2=A0F();<br> &gt; =C2=A0 =C2=A0return Clock.currStdTime() - time;<br> &gt; =C2=A0}<br> &gt; }</p> <p>&gt; The problem with the template is I cannot pass any argument into &q= uot;F&quot;. How should I fix this problem?</p> <p>As Simendsjo said, you can use a function template to get what you want = and a nicer syntax as the same time.</p> <p>Slightly different code can be obtained by starting from the above code:= </p> <p>template timer(alias F) // capture F&#39;s name<br> {<br> =C2=A0=C2=A0=C2=A0 auto timer(Args...)(Args args) // there are your paramet= ers<br> =C2=A0=C2=A0=C2=A0=C2=A0 {<br> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 import std.datetime; // inner im= ports<br> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 auto start =3D Clock.currStdTime= ();<br> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 F(args);<br> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return Clock.currStdTime() - sta= rt;<br> =C2=A0=C2=A0=C2=A0 }<br> }</p> <p>So it&#39;s a template that gets expanded into another template with the= same name, a function.</p> <p>Note that the construct:</p> <p>template (T...)<br> {<br> =C2=A0=C2=A0=C2=A0 T foo(T t) // some function that depends on the template= parameters<br> =C2=A0=C2=A0=C2=A0 {<br> =C2=A0=C2=A0=C2=A0 }<br> }</p> <p>can get syntax sugar like this:</p> <p>T foo(T...)(T t)<br> {<br> }</p> <p>you can &#39;fuse&#39; the external template and the inner code to get a= function template. The same for classes and structs.</p> <p>Now, for something a bit more hairy: Simendsjo&#39;s version and mine ar= e not strictly equivalent:</p> <p>- his version is simpler to type and to reason about. You should use it,= it&#39;s the D way to do this kind of thing.<br> - mine (the two level templates) gets an interesting effect: I can use the = first level only (getting the function name), keeping the second level for = later:</p> <p>// mine<br> alias timer!foo tfoo; // tfoo is the inner template, the function template<= br> /* a bit later */<br> tfoo(args1);<br> tfoo(args2);</p> <p>import std.algorithm;<br> Args[] argsArray;<br> auto m =3D map!(tfoo)(argsArray); // applying tfoo on all arguments groups<= /p> <p>// Simendsjo:<br> timer!foo(args1);<br> timer!foo(args2); // You must provide &#39;foo&#39; for each call.<br><br><= br></p> <p>&gt;<br> &gt; Also, I am not entirely sure why I need to write &quot;alias F&quot; i= nstead of just &quot;F&quot;, any explanation would be appreciated.</p> <p>Because in your case, you want to get the name, the symbol. So use an al= ias parameter. &#39;F&#39; would be to deal with types, as Ali said.</p> <p>For example:</p> <p>auto foo(F, Args...)(F fn, Args args)<br> {<br> ...<br> }</p> <p>in this case, fn is passed as a runtime value of type F:</p> <p>timer(&amp;foo, args); // the compiler automatically deduces the types o= f fn and args.<br></p> <p>Philippe</p> --20cf300fafe5515daf04c33244da--
Jun 23 2012
prev sibling parent "Michael" <pongad gmail.com> writes:
Thank you all of you for your help!

Michael

On Sunday, 24 June 2012 at 06:49:38 UTC, Philippe Sigaud wrote:
 The closest I came to the correct template is
 template(alias F) timer {
  auto timer {
    auto start = Clock.currStdTime();
    F();
    return Clock.currStdTime() - time;
  }
 }

 The problem with the template is I cannot pass any argument 
 into "F". How

As Simendsjo said, you can use a function template to get what you want and a nicer syntax as the same time. Slightly different code can be obtained by starting from the above code: template timer(alias F) // capture F's name { auto timer(Args...)(Args args) // there are your parameters { import std.datetime; // inner imports auto start = Clock.currStdTime(); F(args); return Clock.currStdTime() - start; } } So it's a template that gets expanded into another template with the same name, a function. Note that the construct: template (T...) { T foo(T t) // some function that depends on the template parameters { } } can get syntax sugar like this: T foo(T...)(T t) { } you can 'fuse' the external template and the inner code to get a function template. The same for classes and structs. Now, for something a bit more hairy: Simendsjo's version and mine are not strictly equivalent: - his version is simpler to type and to reason about. You should use it, it's the D way to do this kind of thing. - mine (the two level templates) gets an interesting effect: I can use the first level only (getting the function name), keeping the second level for later: // mine alias timer!foo tfoo; // tfoo is the inner template, the function template /* a bit later */ tfoo(args1); tfoo(args2); import std.algorithm; Args[] argsArray; auto m = map!(tfoo)(argsArray); // applying tfoo on all arguments groups // Simendsjo: timer!foo(args1); timer!foo(args2); // You must provide 'foo' for each call.
 Also, I am not entirely sure why I need to write "alias F" 
 instead of

Because in your case, you want to get the name, the symbol. So use an alias parameter. 'F' would be to deal with types, as Ali said. For example: auto foo(F, Args...)(F fn, Args args) { ... } in this case, fn is passed as a runtime value of type F: timer(&foo, args); // the compiler automatically deduces the types of fn and args. Philippe

Jun 27 2012