Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Tuesday, 4 December 2018

Measuring function time in linux using C


Measuring Time 

So in my job recently i had to time how long it took for a module to be plugged in before that module became "ready" ... the answer was 46 seconds.. but some careful polling changes and a change how firmware was loaded mean't that got chopped in half almost to 26 seconds!

But that's not the point, the point was how did i do such timing.

The original way i found was to use to use clock(). Clock returns an approximation of the processor time used by the program.
Using the following code..

#include <time.h>
#include <stdio.h> 
int main() 
{ 
   clock_t start, stop, length; 
   start = clock(); 
   
   function_to_be_time(parameters); 
   
   stop = clock(); 

   length = (double) stop - start / CLOCKS_PER_SEC; 

   printf("Time for your function was %ld\n", length); 
   
   return; 
} 

Now, this works mind you! but there's a gotchya here, oh yes, you saw that coming didn't you.. go on - admit it..
I probably didn't read the manual (RTFM!) for clock properly.... because the times i was getting back for my function calling didn't make sense compared to time stamped outputs!?
Clock() is returning time used by the program... i know - that didn't make sense, but it does if you mention the word threading.
See i was in a multi-threaded enviroment. So while this was timing my function in this "program" (ie thread!) my "program" had been running differently (in time) from other functions (ie threads!) that made up the entired module "ready" process.

Take 2. 

So instead, i used clock_gettime() with CLOCK_MONOTONIC.
This gave me a much better accuracy across my module insertion process, and started giving me numbers that made sense with other time stamped debug statements.

So.. using the following code,
#include <time.h>
#include <stdio.h>

/* Convert the timespec struct into a seconds value
static double TimeToSeconds(struct timespec* ts)
{
   return (double)ts->tv_sec + (double)ts->tv_nsec / 1000000000.0;
}

int main()
{
    struct timespec start, stop;
    double time_spent = 0;
    clock_gettime(CLOCK_MONOTONIC, &start);

    <YOUR FUNCTION GOES HERE>

    clock_gettime(CLOCK_MONOTONIC, &stop);
    time_spent = TimeToSeconds(&stop) - TimeToSeconds(&start);
    
    printf("your function time was %ld\n", time_spent);

    return;
}

Hopefully that helps someone with timing issues in threads etc.. lemme know if it works for you :)

Lastly - so i don't forget (possibly)... time stamping..
I'm just gonna post the code

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf("Time Stamp is [%d:%d:%d]\n", 
        timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

All good fun :)



Monday, 6 June 2016

Exam rants

Exams... what are they good for... really?

I realize that the academic world uses the exam process as a measure for how much information was squeezed into your brain in the last X period of time.

So they sit you down, give you a piece of paper that you quietly download your cranium onto in the hopes that in brief period of time that you are given a grade that means you know or you don't.

The problem is that.

Lets say you're like me, that sometimes a subject is interesting and that you find it invigorating to learn about it, and hence your grey matter soaks it up like a sponge to a puddle of water.
But - the person next to me finds it totally the opposite, dreary and mind numbingly aweful, and hence their brain refuses.. no matter how much they try.. to accept that this is going to be useful, and therefor prehaps you should learn it.
So - exam comes.. me.. i do really well, thats great.. person next to me does... ok - still manages to pass.

10 years pass by... (watch the tumble weeds go by)...
We both go for the same job, ohh look says mr employer... i see that you sat a course on molecular biophysics, just what we need, can you comment on ....
And here is my problem, i haven't used molecular biophysics in 10 years, i've been a truck driver... but yet - it says on my paper.. i sat an exam - and i got an X for it. At this point, i feel my knowledge is about the same as that person who was next to me....

I know - the argument here is that i was going for a job that needed that.. blah blah blah, but that aside, you see my point.

I bet for most of us, 6 months down the track - the information retained in the membrane isn't at the same level that it was during the 3 hrs that you were hurriedly scratching at a piece of paper for.

So while i can see that an exam is a measure of performance, it just sucks... esp when we get to the real world, and work in fricken TEAMS!

oh well...