Re: Counter-intuitive io vs no-io time readings
On 09-04-2014 17:26, Ian Collins wrote:
> Ney Andr?? de Mello Zunino wrote:
[...]
>> class vec_seq_access : public timed_task {
>> public:
>> vec_seq_access(const std::vector<unsigned int>& data) : data(data) {
>> }
>> protected:
>> virtual void do_run() const {
>> int odd_count = 0;
>> int even_count = 0;
>> for (const auto& i : data) {
>> if (i % 2 != 0) ++odd_count; else ++even_count;
>> }
>> std::cout << odd_count << " odd numbers and " << even_count <<
>> " even
>> numbers.\n";
>
> If this is the line you refer to later, I would expect an optimiser
> to remove the body of this function if you remove this line.
That's a reasonable assumption. Upon realizing that none of the values
produced in this function are actually read/referenced, the optimizer
could decide that no code needed to be generated. However, that would
make it all even more counter-intuitive, since the program output showed
that the time readings were greater for the no-output case, when
compared to the other case, where the output line was kept (and the code
not optimized away, according to the assumption at hand).
>> auto generate_random_data(int count) {
>
> This auto declaration is missing the return type.
Luca Risolia's got it right. I've been away from C++ for a long time and
decided to try out some of its new features.
>> Surprisingly, the average run time has actually increased to about
>> 2.14 seconds. Surely, I must be missing something or maybe there's
>> some weird combination of factors leading to this outcome. Can
>> anybody see what's going on?
>
> Probably the latter! Did you look at the generated assembly code?
>
>> Compiler: g++ 4.8.1
>> Compilation command: g++ --std=c++1y -o ds-perf ds-perf.cpp
I did. I've created a repository on BitBucket and pushed the source
code, assembly outputs and a screenshot of the relevant part of the
comparison between them. I've used '-io' and '-no-io' suffixes to refer
to the instances with the 'cout' line enabled and suppressed, respectively.
https://bitbucket.org/Zunino/ds-performance/src
> --std=c++1y ?
Again, as Luca Risolia's said, it's g++'s option to enable support for
the latest language features.
P.S.: Just one last note to make matters a bit more contrived: I've run
the same tests here in my laptop at home and, guess what? The results
were more like what I expected, that is, the version without the
IO/stream call did perform better. Not by as much as I'd thought, but it
did. Oh, well...
Thank you all for your time.
Regards,
--
Zunino