Get time in milliseconds and check how much time has passed.

Here is my code:
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()
{
    time_t time0;   // create timers.
    time_t time1;

    time(&time0);   // get current time.

    for(int i = 0; i < 2000; i++){std::cout << "i: " << i <<'\n';}  // make computer to do something so time would pass.

    time(&time1);   // get current time after time pass.

    std::cout << "The time1 is: " << time1 <<'\n';
    std::cout << "The time0 is: " << time0 <<'\n';

    double seconds = time1 - time0;

    std::cout << "seconds since start: " << seconds <<'\n';

    return 0;
}


How do i do this in milliseconds?
Why the time in seconds im getting looks like an int but i declared it to be double?

Thank you for your help.
Last edited on
Do you mean something like:
http://www.cplusplus.com/reference/chrono/
http://www.cplusplus.com/reference/chrono/steady_clock/


Why the time in seconds im getting looks like an int but i declared it to be double?

Why did you declare it as a double?
What is the type and unit of time_t?
Yes something like chrono. I coud use it but is there a simpler way? I think that all those duration_cast<duration<double>> will use a lot of cpu power.

What is the type and unit of time_t?
Why did you declare it as a double?
I was using "http://www.cplusplus.com/reference/ctime/time/" they declared it as double at line 9.
You can try QueryPerformanceFrequency();
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <windows.h>

double PCFreq = 0.0;
__int64 CounterStart = 0;

bool StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))return false;

	PCFreq = double(li.QuadPart);

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}

double GetCounterNanoseconds()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

double GetCounterMilliseconds()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq * 1000.f;
}

SYSTEMTIME stime;
LARGE_INTEGER sys_freq;

int main()
{
    QueryPerformanceFrequency(&sys_freq);

    StartCounter(); Sleep(xxxx); cout << GetCounterNanoseconds();

    return 0;
}
Last edited on
Thank you for fixing my title SakurasouBusters.

Your solution looks even more coplicated than keskiverto.
There must be simpler way.
All that it gives you is this :
StartCounter(); Sleep(xxxx); cout << GetCounterNanoseconds();

P.S : This is the best solution if performance is a critical concern.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <chrono>

int main()
{
    auto start = std::chrono::steady_clock::now( );
    
    for( int i = 0; i < 2000; i++ ){ std::cout << "i: " << i << '\n'; }  // make computer to do something so time would pass.
    
    auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now( ) - start );
    
    std::cout << "milliseconds since start: " << elapsed.count( ) << '\n';
}


milliseconds since start: 18
Last edited on
What? Just 2000 increment operations take 18 milliseconds??
@SakurasouBusters
What's so unusual about that?
I think that all those duration_cast<duration<double>> will use a lot of cpu power.

Do not judge the dog by its fur.

There are two considerations:

* Correctness. We can write code so complicated that tracing bugs is a nightmare. A bit simpler implementation, even if it uses a bit more expensive tools is "cheaper" to maintain. Correct code is more important than quick undefined behaviour. While there are many characters in chrono-incantations (and other standard library/latest C++ bits) , they are still quite simple, precise, and to the point (and provides ... well whatever resolution the underlying hardware/OS has). The <ctime> & co. are C-based older alternatives and some of them are officially deprecated.

* Compiler optimizations. Do you know how the standard library has been implemented? Do you know what optimization steps the compiler can do to such code during compilation? The compiler might be able to evaluate some expressions already during compilation so that the binary won't have many instructions to execute during runtime.


If we write something "simple", like auto in integralfx's example, it may not be so simple in reality. On the other hand the long-looking std::chrono::duration_cast<std::chrono::milliseconds> could be a simple "multiply with constant".



@SakurasouBusters:
Your solutions are not standard C++. They are specific to some operating system and will not work everywhere.

You have no idea where integralfx did run the example, nor how loaded that system was.
> What? Just 2000 increment operations take 18 milliseconds??

18 milliseconds of elapsed calendar time does not imply that the operation took 18 milliseconds of elapsed processor time.

std::clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system. For example, if the CPU is shared by other processes, std::clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, std::clock time may advance faster than wall clock. http://en.cppreference.com/w/cpp/chrono/c/clock



> I think that all those duration_cast<> will use a lot of cpu power.

As a general principle, perform the duration_cast<> and other computations on time outside the section of code that is being timed.

1
2
3
4
5
6
7
const auto start = std::chrono::steady_clock::now( );

// do something (section of code that is being timed)

const auto end = std::chrono::steady_clock::now( ); 

const auto duration_msecs = std::chrono::duration_cast<std::chrono::milliseconds>(end-start) ;



> What is the type and unit of time_t? Why did you declare it as a double?

The standard only requires that std::time_t is a suitable arithmetic type capable of representing calendar time. To be (pedantically) correct, use auto or std::time_t to declare variables to hold values returned by std::time and std::difftime (it returns a result of type double) to get the difference between two std::time_t values.
http://en.cppreference.com/w/cpp/chrono/c/difftime
Thank you for all of your help.
From what i can see it looks like chrono is the way to go about this.

integralfx code looks nice i will use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <chrono>

int main()
{
    auto start = std::chrono::steady_clock::now( );
    
    for( int i = 0; i < 2000; i++ ){ std::cout << "i: " << i << '\n'; }  // make computer to do something so time would pass.
    
    auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now( ) - start );
    
    std::cout << "milliseconds since start: " << elapsed.count( ) << '\n';
}
Topic archived. No new replies allowed.