[Issue 13724] std.datetime.timeIt

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Nov 13 07:03:17 PST 2014


https://issues.dlang.org/show_bug.cgi?id=13724

--- Comment #5 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to bearophile_hugs from comment #4)
> (In reply to Steven Schveighoffer from comment #3)
> > You're looking at it from the perspective that the time is the main thing
> > you care about, i.e. you don't care about the return value, just the time it
> > took.
> 
> If I want only the timing I return only the timing. But both this timeIt and
> the Mathematica function return a pair of the result and the time to compute
> it, because that's the most useful result and it's simple to handle and
> remember. If you take an hour to compute something you don't want to repeat
> the computation two times to have both result and timing.

Mathematica is not D :)

I was never saying to repeat the function, so I'm not sure where that came
from.

> > I look at it from the perspective that you want to time something that
> > you are already using.
> > 
> > For example, if you have something like:
> > 
> > 37.fibonacci.foo();
> > 
> > Now, you want to keep your code the same, but get the time it took to do
> > fibonacci, how do you do that with the tuple return?
> > 
> > With a ref parameter, you can do:
> > 
> > ulong timer;
> > 37.fibonacci.timeIt(timer).foo();
> > 
> > Now I have the timer, and I don't have to mess with my call chain (much).
> > 
> > It also allows easier cumulative timing.
> 
> This is a different and more complex usage. It's interesting, but fails at
> being very simple and short. So I am not sure.

I don't know if you have considered the usage if you wanted to use your method:

auto x = 37.fibonacci.timeIt;

foo(x[0]);

Maybe as short as mine, but not simple.

This breaks up your chain. If you were timing multiple things in the chain, you
have to break it wherever timeIt is inserted.

If you wanted cumulative time, you have to take the extra step of adding them
all together.

> 
> Also "timer" in your example is an not-precisely typed variable (raw typed)
> (and it's not a double).

Sure, make it a double then :) I would recommend actually making it a Duration.

> > how do you do that with the tuple return?
> 
> In a functional language you often have "glue" that allows you to solve such
> simple problems in a standard and simple way.

Somehow you have to say "do this with the first part of the tuple, do that with
the second part". I don't think it's very straightforward. In D, if you want to
deal with the tuple in parts, you have to separate the statements, or start
throwing in some helper delegates. It doesn't sound appealing to me.

--


More information about the Digitalmars-d-bugs mailing list