Grabbing Time?

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.new


while (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


Any help is appreciated. Thanks in advance.
Thanks, but how do I make the difference a float? Even I switch the data type to float, it doesn't become one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}


Any idea?
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.

-Albatross
Last edited on
Now I'm keep getting 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <time.h>

int main()
{
    float i = 0, counter1, counter2, fin;
    clock_t clock;    
    counter1 = (clock / CLOCKS_PER_SEC);
     
    while (i != 15000000) 
    {   
    i++; 
    }   
    
    counter2 = (clock / CLOCKS_PER_SEC);
    fin = (counter2 - counter1);
                  
    std::cout << "Finish\nResults: " << fin;
    std::cout << "\nPress ENTER to exit\n";
    std::cin.get();
    std::cin.get();
    return 0;
}


Am I completely off the mark?
You don't seem to call the function clock() and assign its returned value to your data type clock. See here: http://www.cplusplus.com/reference/clibrary/ctime/clock/
Okay I've re-wrote parts again and am getting better results just strange numbered results...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <time.h>

int main()
{
    float fin;
    double i = 0;
    int seconds = 1;
    clock_t counter1;
    counter1 = clock () + seconds * CLOCKS_PER_SEC;     
     
    while (i != 10000000) 
    {   
    i++; 
    }   
    
    clock_t counter2;
    counter2 = clock () + seconds * CLOCKS_PER_SEC;
    fin = (counter2 - counter1);
                  
    std::cout << "Finish\nResults: " << fin;
    std::cout << "\nPress ENTER to exit\n";
    std::cin.get();
    std::cin.get();
    return 0;
}


Which returns 73-98. Is that in milliseconds?

EDIT:

Okay never mind, I got it. Thanks for all of the help.
Last edited on
Topic archived. No new replies allowed.