I was running a benchmark-esque program in Ruby and was wandering if C++ has a time function? If so, how can you grab the time and manipulate it?
Here's what I was doing with Ruby:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
i = 0
time1 = Time.newwhile (i != 10000)
i += 1
end
puts 'Finish'
sleep 0.25
puts 'Results: '
sleep 0.2
time2 = Time.new
fin = (time2 - time1)
puts 'The test took ' + fin.to_s + ' seconds'
puts ''
puts 'Press ENTER to exit'
puts ''
gets.chomp
#include <iostream>
#include <time.h>
int main()
{
double i = 0;
double dif; //could be double or float and either way it's a double
time_t start, end;
time (&start); //Start
while (i != 1000445200)
{
i++;
}
time (&end); //End
dif = difftime (end,start);
std::cout << "Finish\nResults: ";
std::cout << dif;
std::cout << "\nPress ENTER to exit\n";
std::cin.get();
std::cin.get();
return 0;
}
If you want a timer, I recommend using clock(). This returns an int value regarding the number of clock ticks since the program started. There's usually more than one per second. O_o.
Multiply that by some fraction that includes CLOCKS_PER_SEC and convert that to a float to get your float.
time() returns an int value regarding the number of seconds elapsed since a historical time. Seconds.