Incorrect Time in C++ - c++

I have the following code:
clock_t tt = clock();
sleep(10);
tt = clock()-tt;
cout<<(float)tt/CLOCKS_PER_SEC<<" "<<CLOCKS_PER_SEC<<endl;
When I run the code, it apparently pauses for 10 seconds and the output is:
0.001074 1000000
This indicates it passed 1074 clock ticks and 1ms, which is apparently false.
Why does this happen?
I am using g++ under linux.

The function clocks returns the processor time consumed by the program. While sleeping, your process does not use any amount of processing, so this is expected. The amount of time your program is showing could be from the clock function calling.

clock() doesn't measure elapsed time (what you would measure with a stopwatch), it measures the time spent by your program running on the CPU. But sleep() almost don't use any CPU, it simply makes your process going to sleep. Try to modify sleep(10) by any other value sleep(1)for example, and you will get the same result.

Related

What does the timeGetTime() function really return?

I'm wondering what the timeGetTime() function really returns. I've powered on my system about 15 mins ago, and timeGetTime() returns 257052531 milliseconds which is about 71 hours!
The documentation says:
The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.
So, my system is operating ONLY ~15 mins! How could it return ~71 hours!?

MPI time measurement got affected by other user?

I am measuring time in my MPI code like this:
MPI_Barrier(MPI_COMM_WORLD);
MPIt1 = MPI_Wtime();
// my code
MPI_Barrier(MPI_COMM_WORLD);
MPIt2 = MPI_Wtime();
MPIelapsed_inverse = MPIt2 - MPIt1;
I am logging into the lab computer with ssh and I run my program. However, these days I am running a really long experiment (it takes about 1 or 2 days to get completed). Yesterday night, another user logged in and did some heavy tasks too, which resulted in loss of the CPU from mine project for some time.
Will this affect my time measurements, or will MPI_Wtime() still report the actual elapsed time? I mean regardless of the other user.
MPI_Wtime reports 'wall-clock' or 'elapsed' time. If another user's program takes clock cycles from your program then the elapsed time of your program, from start to finish, will increase.

How to interpret the output of boost::timer::cpu_timer on multicore machine?

I use boost::timer::cpu_timer to measure the performance of some algorithm in my application. The example output looks like this:
Algo1 duration: 6.755457s wall, 12.963683s user + 1.294808s system = 14.258491s CPU (211.1%)
From boost cpu_timer documentation:
The output of this program will look something like this:
5.713010s wall, 5.709637s user + 0.000000s system = 5.709637s CPU (99.9%)
In other words, this program ran in 5.713010 seconds as would be
measured by a clock on the wall, the operating system charged it for
5.709637 seconds of user CPU time and 0 seconds of system CPU time, the total of these two was 5.709637, and that represented 99.9 percent
of the wall clock time.
What does the value I obtained mean (211.1%), does it mean that more than two cores were involved in execution of my algorithm ?
What is the meaning of user CPU time and system CPU time ?
What does the value I obtained mean (211.1%), does it mean that more than two cores were involved in execution of my algorithm ?
It means that program used a little bit more than twice as much CPU time as wall time. For that to happen, it must have been running on at least three cores for some of that time.
What is the meaning of user CPU time and system CPU time ?
User CPU time is time when the CPU is running user code. System CPU time is time when the CPU is running system code. When you call a system function, like the function to read from a file, you switch from running user code to running system code until that function returns.

How to repeat a process ( or to set a period of process) in linux?

I have a process that does something and needs to be repeated after a period of 1ms. How can I set period of a process on linux ?
I am using linux 3.2.0-4-rt-amd64 (with RT-Preempt patch) on Intel i7 -2600 CPU (total 8 cores) # 3.40 Ghz.
Basically I have about 6 threads in while loop shown in code and I want threads to be executed at every 1 ms. At the end I want to measure latency of each thread.
So How to set the period 1ms ?
for example in following code, how can I repeat Task1 after every 1ms ?
while(1){
//Task1(having threads)
}
Thank you.
A call to usleep(1000) inside the while loop will do the job, i.e.:
while (1) {
// Task1
usleep(1000); // 1000 microseconds = 1 millisecond
}
EDIT
Since usleep() is already deprecated in favor of nanosleep(), let's use the latter instead:
struct timespec timer;
timer.tv_sec = 0;
timer.tv_nsec = 1000000L;
while (1) {
// Task1
nanosleep(&timer, NULL);
}
Read time(7).
One millisecond is really a small period of time. (Can't you bear with e.g. a ten milliseconds delay?). I'm not sure regular processes on regular Linux on common laptop hardware are able to deal reliably with such a small period. Maybe you need RTLinux or at least real time scheduling (see sched_setscheduler(2) and this question) and perhaps a specially configured recent 3.x kernel
You can't be sure that your processing (inside your loop) is smaller than a millisecond.
You should explain what is your application doing, and what happens inside the loop.
You might have some event loop, consider using ppoll(2), timer_create(2) (see also timer_getoverrun(2)...) and/or timerfd_create(2) and clock_nanosleep(2)
(I would try something using ppoll and timerfd_create but I would accept some millisecond ticks to be skipped)
You should tell us more about your hardware and your kernel. I'm not even sure my desktop i3770K processor, asus P8Z77V motherboard, (3.13.3 PREEMPT Linux kernel) is able to reliably deal with a single millisecond delay.
(Of course, a plain loop simply calling clock_nanosleep, or better yet, using timerfd_create with ppoll, will usually do the job. But that is not reliable...)

c++ alert timer with little cpu load

I want to write a small alert timer on windows using c++ and msvc2010. The timer needs to trigger a status message after a couple of minutes. I know how to check the system time using c++ and I know there is sleep function in windows api. How can I implement a timer with very little cpu load? For example, I don't want to check the system time every couple of milliseconds to trigger the status message when the trigger time is reached. Do I create cpu load, when using things like sleep(600000) in an extra thread or are there more efficient ways to wait a couple of minutes and execute some code afterwards?
You can indeed busy-wait and poll the time. Even a Sleep(1) will be enough that your program will be barely measurable.
I used to do it "back in the day" and even on my PII 233 Mhz running multiple threads doing this it barely made a dent in the CPU usage.
You could create a thread, write a continuous loop inside which you just sleep for the time interval that your trigger needs to run at then print your message. If you need to run it at 2 minutes, why choose multiple small sleep values and check the time? That would be a waste of CPU time.