std::chrono::duration_cast<chrono::nanoseconds> error

std::chrono::duration_cast<chrono::nanoseconds> error

I'm using icc 14.0.0 on Xubuntu 13.04 and std::chrono::duration_cast returns results which are 1000 times too small. Here is the test code:

#include <iostream>
#include <chrono>
#include <thread>using namespace std;
int main()
   auto start = chrono::high_resolution_clock::now();
   auto end = chrono::high_resolution_clock::now();
   cout << "chrono::duration [ns]: " << chrono::duration_cast<chrono::nanoseconds>(end - start).count() << "n";

When built with icc (icc -std=c++11 clock.cpp) it produces:

chrono::duration [ns]: 10057

The same code built with gcc 4.8.1 (g++-4.8 -std=c++11 clock.cpp) produces a correct result:

chrono::duration [ns]: 10058344

The same problem occures when cast is made to microseconds or milliseconds.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I don't have Xubuntu, I tried your program with gcc 4.8.1 headers and icc on RHEL with intel64 compiler and the results didn't have a scale problem. Could you preprocess your program with both icc and gcc and attach both files? e.g. icpc -c -std=c++0x -E clock.cpp > clock-iccE.cpp ; and the same command using g++.  Also, could you attach the assembly from both gcc and icpc? (Use -S switch when compiling).  If you use the -# switch on icpc compilation line, it will also show the library search path (and a lot of other details!).

Thank you for the tip. I have both gcc 4.8.1 and 4.7 installed on my system and icc is using the default gcc 4.7 libraries, which are the source of std::chrono problem. Since I need both versions of gcc, is there an easy way to configure icc to use gcc 4.8.1 libraries (non-default)?

I found a solution by lucky accident. I was playing with optimization options and when I used -fast, problems with std::chrono disappeared. With -Ofast the problem was still there. My guess is that -fast forces -xHost, which causes different CPU clocks to be used (my CPU supports AVX) and it automagically works now.

Sounds like you solved your problem, thanks for your reply.

When I need to use a different version of gcc/++, I modify the PATH environment variable.

Here's a discussion here about some icc compiler options which are related:

Leave a Comment

Please sign in to add a comment. Not a member? Join today