Thread overview
StopWatch problem
Dec 05, 2017
Joel
Dec 05, 2017
Jonathan M Davis
Dec 05, 2017
Joel
Dec 05, 2017
Ali Çehreli
Dec 05, 2017
Jonathan M Davis
December 05, 2017
void main() {
	import std.datetime: Duration, msecs;
	import std.datetime.stopwatch: StopWatch;

	StopWatch sw;
	if (sw.peek.msecs) {
		
	}
}

I get this error with the code:
z.d(6): Error: function core.time.dur!"msecs".dur (long length) is not callable using argument types (Duration)
December 05, 2017
On Tuesday, December 05, 2017 21:33:53 Joel via Digitalmars-d-learn wrote:
> void main() {
>   import std.datetime: Duration, msecs;
>   import std.datetime.stopwatch: StopWatch;
>
>   StopWatch sw;
>   if (sw.peek.msecs) {
>
>   }
> }
>
> I get this error with the code:
> z.d(6): Error: function core.time.dur!"msecs".dur (long length)
> is not callable using argument types (Duration)

core.time.msecs is an alias for core.time.dur!"msecs". It takes a long for the number of milliseconds and returns a Duration. If you want to convert a Duration to milliseconds, then use its member function, total. e.g. sw.peek.total!"msecs". It will convert the hnsecs in the duration to msecs using integral math and return a long.

If what you want is a floating point value like TickDuration.msecs does,
then you'll have to do the math yourself with something like
sw.peek.total!"hnsecs" / cast(real)convert!("seconds", "hnsecs")(1).

https://dlang.org/phobos/core_time.html#.msecs https://dlang.org/phobos/core_time.html#.Duration.total

- Jonathan M Davis

December 05, 2017
On Tuesday, 5 December 2017 at 21:45:20 UTC, Jonathan M Davis wrote:
> On Tuesday, December 05, 2017 21:33:53 Joel via Digitalmars-d-learn wrote:
>> [...]
>
> core.time.msecs is an alias for core.time.dur!"msecs". It takes a long for the number of milliseconds and returns a Duration. If you want to convert a Duration to milliseconds, then use its member function, total. e.g. sw.peek.total!"msecs". It will convert the hnsecs in the duration to msecs using integral math and return a long.
>
> If what you want is a floating point value like TickDuration.msecs does,
> then you'll have to do the math yourself with something like
> sw.peek.total!"hnsecs" / cast(real)convert!("seconds", "hnsecs")(1).
>
> https://dlang.org/phobos/core_time.html#.msecs https://dlang.org/phobos/core_time.html#.Duration.total
>
> - Jonathan M Davis

Got it, thanks.
December 05, 2017
On 12/05/2017 01:45 PM, Jonathan M Davis wrote:
> On Tuesday, December 05, 2017 21:33:53 Joel via Digitalmars-d-learn wrote:
>> void main() {
>>    import std.datetime: Duration, msecs;
>>    import std.datetime.stopwatch: StopWatch;
>>
>>    StopWatch sw;
>>    if (sw.peek.msecs) {
>>
>>    }
>> }
>>
>> I get this error with the code:
>> z.d(6): Error: function core.time.dur!"msecs".dur (long length)
>> is not callable using argument types (Duration)
>
> core.time.msecs is an alias for core.time.dur!"msecs". It takes a long for
> the number of milliseconds and returns a Duration. If you want to convert a
> Duration to milliseconds, then use its member function, total. e.g.
> sw.peek.total!"msecs". It will convert the hnsecs in the duration to msecs
> using integral math and return a long.
>
> If what you want is a floating point value like TickDuration.msecs does,
> then you'll have to do the math yourself with something like
> sw.peek.total!"hnsecs" / cast(real)convert!("seconds", "hnsecs")(1).
>
> https://dlang.org/phobos/core_time.html#.msecs
> https://dlang.org/phobos/core_time.html#.Duration.total
>
> - Jonathan M Davis
>

Selective imports complicates matters. Changing the imports lets it compile with 2.076:

void main() {
    import std.datetime;
    import std.stdio: writeln;

    StopWatch sw;
    writeln(sw.peek.msecs);
}

Ali

December 05, 2017
On Tuesday, December 05, 2017 14:25:12 Ali Çehreli via Digitalmars-d-learn wrote:
> Selective imports complicates matters. Changing the imports lets it compile with 2.076:
>
> void main() {
>      import std.datetime;
>      import std.stdio: writeln;
>
>      StopWatch sw;
>      writeln(sw.peek.msecs);
> }

Yes, and then you'll get deprecation warnings with 2.077. The cleanest way to deal with replacing the TickDuration versions of the benchmarking stuff with the MonoTime/Duration versions was to put the new ones in a module that isn't imported by std/datetime/package.d and leave the old functions in package.d (the whole reason that the old benchmarking stuff wasn't replaced sooner was because it required splitting std.datetime first to do it in even a semi-clean manner). Once they've gone through the full deprecation process and are gone, then std.datetime.stopwatch can be publicly imported in package.d, and the import mess will be clean again. But fortunately, if you do

import std.datetime.stopwatch : StopWatch;
import std.datetime;

everything works just fine, because the module system then prefers the one that's selectively imported. So, it's much less of an import mess than it would be otherwise. I was surprised at how cleanly D's module system deals with the potential conflict.

- Jonathan M Davis