Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #89027

Re: What does "current time" mean?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: What does "current time" mean?
Date 2023-02-07 23:16 +0100
Organization A noiseless patient Spider
Message-ID <truij2$3t2f2$1@dont-email.me> (permalink)
References <tru716$3r5bl$1@dont-email.me> <87a61pdzkq.fsf@nosuchdomain.example.com>

Show all headers | View raw


On 07/02/2023 20:51, Keith Thompson wrote:
> Manu Raju <MR@invalid.invalid> writes:
>> Some of you might be interested in this paper by Amazon Engineer:
>>
>> <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0342r1.pdf>
> 
>  From the paper:
> 
>      auto start = chrono::high_resolution_clock::now();
>      auto result = fib(which);
>      auto end = chrono::high_resolution_clock::now();
>      ...
> 
>      I was surprised to discover that the program printed an elapsed time
>      of zero milliseconds on at least one compiler. Looking at the
>      generated code at https://godbolt.org/z/vY5d9bref revealed that the
>      compiler had reordered the code to get the end time prior to running
>      the calculation.
> 
> I'm surprised such a transformation would be legal.
> 

(Note - I haven't read the paper yet, just this post.)

It seems perfectly reasonable to me.  Neither C nor C++ care about 
timing, nor the ordering of anything except observable behaviour.  The 
compiler can see that "fib" does not have any observable behaviour - it 
is a "pure" function.  It doesn't matter when the calculation is done, 
as far as the observable behaviour is concerned, so it is fine to move 
it before or after the calls to now() if that gives more efficient code.

It would also be fine for the compiler to pre-calculate fib(42) at 
compile time and skip the run-time calculation entirely.

I see this kind of misunderstanding regularly when people try to make 
benchmarks.  Usually the solution is to put "volatile" in the right 
place - force observable behaviour to force the ordering by making 
"which" and "result" volatile.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What does "current time" mean? Manu Raju <MR@invalid.invalid> - 2023-02-07 19:00 +0000
  Re: What does "current time" mean? scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 19:39 +0000
    Re: What does "current time" mean? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 12:00 -0800
      Re: What does "current time" mean? Vir Campestris <vir.campestris@invalid.invalid> - 2023-02-09 12:11 +0000
        Re: What does "current time" mean? David Brown <david.brown@hesbynett.no> - 2023-02-09 14:13 +0100
  Re: What does "current time" mean? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 11:51 -0800
    Re: What does "current time" mean? David Brown <david.brown@hesbynett.no> - 2023-02-07 23:16 +0100
  Re: What does "current time" mean? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 04:19 -0800
  Re: What does "current time" mean? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-09 05:27 -0800
  Re: What does "current time" mean? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-02-10 20:53 -0800

csiph-web