I've been learning C++ for last couple of days. I must say I briefly met with C++ during my university years (1995 - 2001), but I've always considered myself more of a Java programmer. In fact I've been doing 100% Java for the last 5 years developing server applications.
My newly aquired hobby in genetic algorithms will probably require me to program them in C++. I've tried Java, but I have to make it faster.
Of course I wanted to see how fast parts of the code are in Java and in C++. If you want to see that in Java you just write:
long beginTime = System.currentTimeMillis();
..... code you want to measure
long endTime = System.currentTimeMillis();
System.out.println("Code took " + (endTime - beginTime) + " ms to execute");
this way you get fairly good feeling what parts of your code are slow. Of course you could use Profilers, but usually I don't.
Well.. I tried the same thing in C++. Guess what. You cannot do that. There is no elegant way to do that. You cannot get time in milliseconds/nanoseconds in C++. You have to use a hack (IMHO).
there is a function clock() that returns how many CPU ticks have elapsed. Then your code looks something like:
int startTime = clock() / CLOCKS_PER_SEC;
..... code you want to measure
int endTime = (clock() / CLOCKS_PER_SEC) - startTime;
Ugly if you ask me!!!
If anyome has a better solution, please come forward. :)
Ni komentarjev:
Objavite komentar