On Wednesday, December 06, 2017 09:12:08 helxi via Digitalmars-d-learn wrote: > void main() > { > import std.stdio, std.datetime; > > auto t0 = Clock.currTime; > writeln(fib(100_000)); > writeln(Clock.currTime - t0); > }
On a complete sidenote, if you want to correctly time stuff, you should use a monotonic clock rather than the wall clock, since the wall clock can be shifted by the system, whereas a monotonic clock is guaranteed to always move forward at a fixed rate. So, you'd either want to do something like import std.stdio, std.datetime; auto t0 = MonoTime.currTime; writeln(fib(100_000)); writeln(MonoTime.currTime - t0); or import std.stdio, std.datetime.stopwatch; auto sw = StopWatch(AutoStart.yes); writeln(fib(100_000)); writeln(sw.peek); That way, you don't have to worry about the timing being wrong due to the clock being shifted. Such shifting happens infrequently enough that it won't _usually_ create a problem, but it can create a serious problem if a shift occurs and you're doing something like waiting for the clock to reach a certain point in time. It's just better to get in the habit of using the monotonic clock for timing stuff. Note however that for the time being, if you want to use anything in std.datetime.stopwatch, you'll need to either just import it and not std.datetime or use selective import such as import std.datetime.stopwatch : StopWatch; This is because the old versions of the benchmarking stuff like benchmark and StopWatch are in std.datetime.package and have been deprecated (they use core.time.TickDuration which will soon be deprecated) and replaced with nearly identical versions in std.datetime.stopwatch which use core.time.MonoTime and core.time.Duration. Until the old versions have gone through the full deprecation cycle and have been removed, they'll conflict with the new ones, so std.datetime.stopwatch is not yet publicly imported in std.datetime.package, and if you import both, then selective imports are needed in order to deal with the symbol conflict. - Jonathan M Davis