Divine
Divine

Reputation: 29

How to find execution time (if possible in nanoseconds) of the sections in a C code on linux with intel dual core?

I have a C code with some functions.I need to find out the execution time of each function, I have tried using gettimeofday and rdtsc,but i guess that because its multi core system the output time provided involves the switching time between the processors. I wanted it to be serialized. So if somebody can give me an idea that how should i calulate the time or at least let me know about the syntax of rdstcp.

P.S. please reply as soon as possible Thanks :)

Upvotes: 2

Views: 2431

Answers (8)

Siva
Siva

Reputation: 141

Use the struct timespec structure & clock_gettime function as follows to obtain the time of execution of the code in nanoseconds precision

struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);

It returns a value as ((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))

Moreover this I've used for multithreaded concepts too..

Hope this answer will be more helpful for you to get your desired execution time in nanoseconds.

Upvotes: 0

Lothar
Lothar

Reputation: 13067

Precise Performance Measuring was impossible until Linux kernel 2.6.31. In this kernel a new library for accessing the performacne counters of the CPU and IMHO correcting times in the scheduler was added.

Unfortunately i don't have more details but maybe it is a starting point for more information search. I'm just adding this because nobody mentioned it before

Upvotes: 0

philant
philant

Reputation: 35816

You could boot your dual core system to use one core only using the following kernel parameter:

maxcpus=1

But the measured time will still comprise process contest switching and thus depend on the activity on the other processes on the system. Are you interested in the execution time, or the CPU time needed to execute your task ?

Upvotes: 1

Pete Kirkham
Pete Kirkham

Reputation: 49311

If you have multiple cores, then the CPU timer won't be stable between them. So set the thread affinity to keep it on the one core. You also might want to use a real time timer to measure the time for the process or thread using clock_gettime(CLOCK_PROCESS_CPUTIMER_ID). Read the note for SMP systems in the usage for that function.

Both of these will effect the timing of the program, so perform multiple iterations of whatever you are benchmarking, and don't call the timing functions too often to try and mitigate this.

Upvotes: 2

jcoder
jcoder

Reputation: 30035

There should be some way to set processor affinity to tell the operating system to only run that thread on a particuar core.

In windows there is a SetThreadAffinity system call, I imagine there is a similar function in linux, although I don't know what it is called.

Upvotes: 1

mikelong
mikelong

Reputation: 3854

Pavium is correct, the only way to get decent timing at this resolution in with an oscilloscope and toggling GPIO pins.
Bear in mind that this is all a bit academic anyway: I suppose you are running with an operating system, etc, so there is no way to get a straight run at the hardware.

You really need to look at the reason you want this measurement. Is it a performance benchmark for some code? You could try running the code many thousands of times and get some statistics. For this kind of approach I would recommend you read Zed Shaws diatribe to make sure the numbers aren't fooling you.

Upvotes: 0

pavium
pavium

Reputation: 15118

It's a little impractical to expect nanosecond resolution.

You can't add code just to output the execution times of functions without increasing execution time. When you take the code out, the timing changes.

In practice, this kind of measurement is made by observing the CPU timing signals on an oscilloscope (or logic analyser).

Upvotes: 3

Clash
Clash

Reputation: 5025

Mate, I'm not sure about this, but even if you're dual core,unless the program is threaded, it will only run in 1 thread (meaning 1 core), so it should not involve the time of switching between processors, I believe there is no such thing...

Upvotes: 0

Related Questions