Counting time in C++/CLI - c++

I have app, where i must count time of executing part of C++ function and ASM function. Actually i have problem, times which i get are weird - 0 or about 15600. O ocurs more often. And sometimes, after executing, times looks good, and values are different than 0 and ~15600. Anybody knows why it occurs ? And how to fix it ?
Fragment of counting time for executing my app for C++:
auto start = chrono::system_clock::now();
for (int i = 0; i < nThreads; i++)
xThread[i]->Start(i);
for (int i = 0; i < nThreads; i++)
xThread[i]->Join();
auto elapsed = chrono::system_clock::now() - start;
long long milliseconds = chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cppTimer = milliseconds;

What you're seeing there is the resolution of your timer. Apparently, chrono::system_clock ticks every 1/64th of a second, or 15,625 microseconds, on your system.
Since you're in C++/CLI and have the .Net library available, I'd switch to using the Stopwatch class. It will generally have a much higher resolution than 1/64th of a second.

Looks good to me. Except for cast to std::chrono::microseconds and naming it milliseconds.
The snippet I have used for many months now is:
class benchmark {
private:
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::milliseconds milliseconds;
clock::time_point start;
public:
benchmark(bool startCounting = true) {
if(startCounting)
start = clock::now();
}
void reset() {
start = clock::now();
}
// in milliseconds
double elapsed() {
milliseconds ms = std::chrono::duration_cast<milliseconds>(clock::now() - start);
double elapsed_secs = ms.count() / 1000.0;
return elapsed_secs;
}
};
// usage
benchmark b;
...
cout << "took " << b.elapsed() << " ms" << endl;

Related

measuring elapsed seconds using chrono (stop watch) C++

I am currently trying to create a way to display the elapsed seconds (not the difference between cycles). My code is following:
#include <iostream>
#include <vector>
#include <chrono>
#include <Windows.h>
typedef std::chrono::high_resolution_clock::time_point TIME;
#define TIMENOW() std::chrono::high_resolution_clock::now()
#define TIMECAST(x) std::chrono::duration_cast<std::chrono::duration<double>>(x).count()
int main()
{
std::chrono::duration<double> ms;
double t = 0;
while (1)
{
TIME begin = TIMENOW();
int c = 0;
for (int i = 0; i < 10000000; i++)
{
c += i*100000;
}
TIME end = TIMENOW();
ms= std::chrono::duration_cast<std::chrono::duration<double>>(end - begin);
t =t+ ms.count();
std::cout << t << std::endl;
}
I expected adding the delta time over and over again to roughly give me the elapsed time in seconds, however I noticed that only if I do i < big number it sort of is fairly accurate. If its only 10,000 or so, t seems to accumulate slower and gradually faster. Maybe I am missing something but isnt the difference my delta time(the elapsed time between this and last cycle) and if I keep adding the delta times up, it should spit out seconds? Any help is appreciated.

How can I test two algorithms and determine which is faster?

Whenever working on a specific problem, I may come across different solutions. I'm not sure how to choose the better of the two options. The first idea is to compute the complexity of the two solutions, but sometimes they may share the same complexity, or they may differ but the range of the input is small that the constant factor matters.
The second idea is to benchmark both solutions. However, I'm not sure how to time them using c++. I have found this question:
How to Calculate Execution Time of a Code Snippet in C++ , but I don't know how to properly deal with compiler optimizations or processor inconsistencies.
In short: is the code provided in the question above sufficient for everyday tests? is there some options that I should enable in the compiler before I run the tests? (I'm using Visual C++) How many tests should I do, and how much time difference between the two benchmarks matters?
Here is an example of a code I want to test. Which of these is faster? How can I calculate that myself?
unsigned long long fiborecursion(int rank){
if (rank == 0) return 1;
else if (rank < 0) return 0;
return fiborecursion(rank-1) + fiborecursion(rank-2);
}
double sq5 = sqrt(5);
unsigned long long fiboconstant(int rank){
return pow((1 + sq5) / 2, rank + 1) / sq5 + 0.5;
}
Using the clock from this answer
#include <iostream>
#include <chrono>
class Timer
{
public:
Timer() : beg_(clock_::now()) {}
void reset() { beg_ = clock_::now(); }
double elapsed() const {
return std::chrono::duration_cast<second_>
(clock_::now() - beg_).count(); }
private:
typedef std::chrono::high_resolution_clock clock_;
typedef std::chrono::duration<double, std::ratio<1> > second_;
std::chrono::time_point<clock_> beg_;
};
You can write a program to time both of your functions.
int main() {
const int N = 10000;
Timer tmr;
tmr.reset();
for (int i = 0; i < N; i++) {
auto value = fiborecursion(i%50);
}
double time1 = tmr.elapsed();
tmr.reset();
for (int i = 0; i < N; i++) {
auto value = fiboconstant(i%50);
}
double time2 = tmr.elapsed();
std::cout << "Recursion"
<< "\n\tTotal: " << time1
<< "\n\tAvg: " << time1 / N
<< "\n"
<< "\nConstant"
<< "\n\tTotal: " << time2
<< "\n\tAvg: " << time2 / N
<< "\n";
}
I would try compiling with no compiler optimizations (-O0) and max compiler optimizations (-O3) just to see what the differences are. It is likely that at max optimizations the compiler may eliminate the loops entirely.

Calculating Running Time of Binary Search

The following Binary Search program is returning a running time of 0 milliseconds using GetTickCount() no matter how big the search item is set in the given list of values.
Is there any other way to get the running time for comparison?
Here's the code :
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char **argv)
{
long int i = 1, max = 10000000;
long int *data = new long int[max];
long int initial = 1;
long int final = max, mid, loc = -5;
for(i = 1; i<=max; i++)
{
data[i] = i;
}
int range = final - initial + 1;
long int search_item = 8800000;
cout<<"Search Item :- "<<search_item<<"\n";
cout<<"-------------------Binary Search-------------------\n";
long int start = GetTickCount();
cout<<"Start Time : "<<start<<"\n";
while(initial<=final)
{
mid=(initial+final)/2;
if(data[mid]==search_item)
{
loc=mid;
break;
}
if(search_item<data[mid])
final=mid-1;
if(search_item>data[mid])
initial=mid+1;
}
long int end = GetTickCount();
cout<<"End Time : "<<end<<"\n";
cout << "time: " << double(end - start)<<" milliseconds \n";
if(loc==-5)
cout<<" Required number not found "<<endl;
else
cout<<" Required number is found at index "<<loc<<endl;
return 0;
}
Your code looks like this:
int main()
{
// Some code...
while (some_condition)
{
// Some more code...
// Print timing result
return 0;
}
}
That's why your code prints zero time, you only do one iteration of the loop then you exit the program.
Try to use the clock_t object from the time.h header:
clock_t START, END;
START = clock();
**YOUR CODE GOES HERE**
END = clock();
float clocks = END - START;
cout <<"running time : **" << clocks/CLOCKS_PER_SEC << "** seconds" << endl;
CLOCKS_PER_SEC is a defined var to convert from clock ticks to seconds.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
This article says that result of GetTickCount will wrap to zero if you system runs for 49.7 days.
You can find here: Easily measure elapsed time how to measure time in C++.
You can use time.h header
and do something like this in your code :
clock_t Start, Stop;
double sec;
Start = clock();
//call your BS function
Stop = clock();
Sec = ((double) (Stop - Start) / CLOCKS_PER_SEC);
and print the sec!
I hope this helps you!
The complexity of binary search is log2(N), it's about 23 for N = 10000000.
I think its not enough to mesure in realtime scale and even clock.
In this case you should use unsigned long long __rdtsc(), that returns number of processor ticks from last reset. Put this before and after your binary search and place cout << start; after obtaining end time. Overwise time of output would be included.
There is also memory corruption around data array. Index in C runs from 0 to size - 1, so thereis no data[max] element.
And delete [] data; before calling return.

Time for an algorithm to run

I'm trying to time my algorithm but the time always shows up as zero. Here is my code:
steady_clock::time_point start = steady_clock::now();
ReverseArray(list1, 1000000);
steady_clock::time_point end = steady_clock::now();
auto totalTime = end - start;
cout << duration_cast<microseconds>(totalTime).count()<< "\n";
Just to elaborate on Jack's answer: on Windows, using Visual Studio up to 2013, there is a bug in the standard library which makes chrono clocks unprecise. The high resolution clock is an alias to the steady clock which is ok, but the bug is that it have a very low resolution (around 8 ms last time I checked).
Therefore, to this day (04/2014), VS standard library can't be used for measuring time or even for time-sensitive concurrent data sharing mechanisms (like when relying on condition_variable.wait_for() or this_thread::sleep_for() functions). Personally I fixed this problem by using Boost implementation instead until it's fixed. As pointed by STL (the person who is in charge of the standard library in Microsoft) in the bug report this should be fixed for the Visual Studio versions higher than 2013 (not the CTPs as they don't change the standard library).
What I mean is that your code is actually correct and cross-platform (except I would have used high_precision_clock instead), that specific implementation is buggy, but it works well with other implementations (GCC, Clang, Boost version of chrono and thread).
For example this works perfectly well on GCC 4.8 (Coliru):
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
auto my_array = []{
std::vector<long long> values( 100000 );
long long last_value = 0;
for( auto& value : values )
value = last_value++;
return values;
}();
void some_work()
{
for( int i = 0; i < 10000; ++i ) // play with this count
std::reverse( begin(my_array), end(my_array) );
}
int main()
{
using namespace std;
using namespace chrono;
auto start = high_resolution_clock::now();
some_work();
auto end = high_resolution_clock::now();
auto totalTime = end - start;
cout << duration_cast<microseconds>(totalTime).count()<< " microsecs \n";
return 0;
}
Coliru return "95 microsecs" for me when trying with only one cycle in some_work().
Maybe you need more resolution:
auto t1 = std::chrono::high_resolution_clock::now();
// Your code to be measured
auto t2 = std::chrono::high_resolution_clock::now();
auto t = std::chrono::duration<double,std::milli>(t2-t1).count();
std::cout << "Time (ms):" << t << std::endl;
If you're on Windows, use this resource.
It is good for sub-microsecond accuracy. Use this:
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
ReverseArray(list1, 1000000);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;

How To record total timing for C++ repetive Iterative function?

I have this function prototype code for factorial calculation by iteration
How do I include a timer to produce total time spent for looping 100 times of the function?
for (unsigned long i=number; i>=1; i--) result *=i;
My C++ knowledge is barely basic, so not sure if "loop" is correctly mentioned here.
However, I was hinted to use .
Pls advice
thank you
Here's a proper C++11 version of some timing logic:
using namespace std;
using namespace chrono;
auto start_time = system_clock::now();
// your loop goes here:
for (unsigned long i=number; i>=1; i--) result *=i;
auto end_time = system_clock::now();
auto durationInMicroSeconds = duration_cast<microseconds>(end_time - start_time);
cout << "Looping " << number << " times took " << durationInMicroSeconds << "microseconds" << endl;
Just for sport, here's a simple RAII-based variation:
class Timer {
public:
explicit Timer(const string& name)
: name_(name)
, start_time_(system_clock::now()) {
}
~Timer() {
auto end_time = system_clock::now();
auto durationInMicroSeconds = duration_cast<microseconds>(end_time - start_time);
cout << "Timer: " << name << " took " << durationInMicroSeconds << "microseconds" << endl;
}
private:
string name_;
system_clock::time_point start_time_;
};
Sure, it's a bit more code, but once you have that, you can reuse it fairly efficiently:
{
Timer timer("loops");
// your loop goes here:
for (unsigned long i=number; i>=1; i--) result *=i;
}
If you looking for time spent in executing number of looping statements in the program code try making use of gettimeofday() as below,
#include <sys/time.h>
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* Your loop code to execute here */
gettimeofday(&tv2, NULL);
printf("Time taken in execution = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
This solution is more towards C which can be employed in your case to calculate time spent.
This is a perfect situation for a lambda. Honestly I don't know the syntax in C++ but it should be something like this:
duration timer(function f) {
auto start = system_clock::now();
f();
return system_clock::now() - start;
}
To use it, you wrap your code in a lambda and pass it to the timer. The effect is very similar to #Martin J.'s code.
duration code_time = timer([] () {
// put any code that you want to time here
}
duration loop_time = timer([] () {
for (unsigned long i=number; i>=1; i--) {
result *=i;
}
}