Built in function clock() not working in atmel studio 7.0 - c

I am working on a stopwatch project and I need to read the time that has passed while the program is running and build my time base from that.
I've included the time.h library and even put the .h file in my project directory but for some reason once I use clock() function my code doesn't build correctly on this or any of my atmel 7 projects.
I included a simple coded that I believe should compile, as well as the errors I get when I try and build. I suspect the problem has something to do with atmel 7, but any other suggestions would be appreciated.
#include <time.h>
#include <avr/io.h>
#include <stdio.h>
int main()
{
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);
printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %ld\n", total_t );
printf("Exiting of the program...\n");
return(0);
}
ERRORS:
recipe for target 'clocktest3.elf' failed
undefined reference to 'clock'
id returned 1 exit status

It obviously don't work because there is no clock source in your AVR system.
What you have to do is to enable one timer, for example TIMER0 and configure it as 1ms ticking and then process values in interrupts or simply read current count. But keep in mind that timer can overflow (8-bit or 16-bit timer) very fast.

this page atmel 7 indicates that the chip must have the RTC module. Does the chip your using have that module?
the following (modified) code:
cleanly compiles
demonstrates that the appropriate answer is a small fraction not some integer number
and now the code:
#include <time.h>
//#include <avr/io.h>
#include <stdio.h>
int main()
{
clock_t start_t, end_t;
double total_t;
int i;
start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);
printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %lf\n", total_t );
printf("Exiting of the program...\n");
return(0);
}
The output, on my computer, of the above code is:
Starting of the program, start_t = 498
Going to scan a big loop, start_t = 498
End of the big loop, end_t = 33075
Total time taken by CPU: 0.032577
Exiting of the program...
So it seems the expectation of the OPs posted code and reality are off by (at least) two orders of magnitude.
There is no way the OPs posted output can be displayed when the OPs posted code does not link.
BTW: here is what the OPs posted code outputs before the logic/math corrections.
Starting of the program, start_t = 473
Going to scan a big loop, start_t = 473
End of the big loop, end_t = 33022
Total time taken by CPU: 0
Exiting of the program...
Note the 0 for the "Total time taken by CPU"

Related

C: How to reset the reurn value of clock() to 0?

I'd like to know if there is a way to reset the return value of the clock() function to 0.
I have a code something like this:
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
int main()
{
/* clock_t t1; */
unsigned int sec = 0;
while(true) {
if(clock() >= 1000) {
printf("%u seconds has passed\r", sec);
/* reset clock()'s return value to 0 */
sec++;
}
}
return 0;
}
what code should I put to the comment's place to reset the timer? Is there a way, or am I aproaching the problem in the incorrect manner?
clock() is always increasing.
The unit of clock is in CLOCKS_PER_SEC. One second has CLOCKS_PER_SEC clocks.
Note that clock() does not measure real time. clock() measures the processor time spend in your process. If you want to measure real time, use time() from time.h (or check your OS, on linux you can use clock_gettime(CLOCK_MONOTONIC, ...) or with CLOCK_REALTIME).
Save the current clock in a variable. Then compare the variable with current clock.
Usually stdout is line buffered. So until you write a newline character, nothing will show up. Make sure to flush stdout if you depend on that behavior.
#include <stdio.h>
#include <time.h>
int main() {
unsigned int sec = 0;
// we will stop the clock one second from now
clock_t stopclock = clock() + 1 * CLOCKS_PER_SEC;
while(1) {
// current time is greater then the stopping time
if (clock() > stopclock) {
// increment stopping time by one second
stopclock += 1 * CLOCKS_PER_SEC;
printf("\r%u seconds has passed", sec);
fflush(stdout);
sec++;
}
}
return 0;
}
Note: calculations on clock_t type like clock() + 1 * CLOCKS_PER_SEC can potentially overflow - great code would handle such corner cases.
My best guess is that you are trying to time something
Take a look at this
#include <time.h>
#include <stdio.h>
int main () {
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);
printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(i=0; i< 1000000000; i++) {
}
end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %f\n", total_t );
printf("Exiting of the program...\n");
return(0);
}

Clock function usleep not working right in c program ?

I want to delay my program for 500 msec using clock_t type .For example i have following code :
#include <time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
clock_t start_t, end_t, total_t,start_2,end_2,total_2;
int i;
start_t = clock();
printf("start_t = %ld\n", start_t);
//do a loop
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("end_t = %ld \n", end_t);
total_t = (double)(end_t - start_t)*1000 / CLOCKS_PER_SEC;
printf("Total1 in msec %ld \n",total_t);
start_2=clock();
printf("start_2 %ld\n ", start_2);
usleep(500000);
end_2=clock();
printf("end_2 %ld\n ", end_2);
total_2=(double)(start_2-end_2)*1000/CLOCKS_PER_SEC;
printf("Total2 in msec %ld\n ", total_2);
return(0);
}
I have found out that usleep takes a value of μsec as an input. So i put 500000.
But i get :
start_t = 1041
end_t = 50441
Total1 in msec 49
start_2 50446
end_2 50478
Total2 in msec 0
What goes wrong ? Why is total2 =0 ? Any help please ?
Found a solution . I wrote a delay function :
void delay (unsigned int msecs) {
clock_t goal = msecs*CLOCKS_PER_SEC/1000 + clock(); //convert msecs to clock count
while ( goal > clock() ); // Loop until it arrives.
}
In fact, clock function records cpu cycle number for this process. When you call the usleep function, the cpu will not provide any time slide for this process.
So the real reason is clock() function. Maybe you can find some functions to get the system time. I think it will work.

time execution can show up

#include <conio.h>
#include <stdio.h>
#include<time.h>
double multi();
void main()
{
clrscr();
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
multi();
//printf("Answer (%d)",s);
}
clock_t end = clock();
float diff;
diff = (float) (end - start) / CLOCKS_PER_SEC;
printf("time execution :%f", diff);
getch();
}
double multi()
{
double a;
a = 5 * 5;
return a;
}
The execution time appear as 0.000000 what the problem!
would it be cause of the nanoseconeds
The man for the clock() function says:
The clock() function returns an approximation of processor time used by the program.
Approximation, so it's not going to be exact, it depends on the granularity of your system. So for starters you can check the granularity of clock() on your system with something like:
clock_t start =clock(), end;
while(1)
{
if(start != (end=clock()))
break;
}
diff=(float)(end - start)/CLOCKS_PER_SEC;
printf("best time :%f",diff);
Doing this for me, I get 0.001 (which is 1ms), so anything that takes less that 1ms to do I will get back "0" instead. That's what's happening to you, your code is running faster than clock()s granularity and so you're getting back the best approximation which happens to be "0"

Clock() return value 0 in usleep loop with more orders

I'm trying to see how much time cost execute some code in a thread. But clock() is returning 0.
This is the code:
int main(int argc, char *argv[])
{
int begin, end;
float time_spent;
clock_t i,j;
struct timeval tv1;
struct timeval tv2;
for(i = 0; i<6; i++)
{
begin = clock();
// Send Audio Data
....
gettimeofday(&tv1,NULL);
usleep(200000); // Wait 200 ms
gettimeofday(&tv2,NULL);
printf("GETTIMEOFDAY %d\n", tv2.tv_usec-tv1.tv_usec); // Time using date WORKING
end = clock() - begin;
// Store time
...
printf ("It took me %d clicks (%f seconds).\n",begin,((float)begin)/CLOCKS_PER_SEC);
printf ("It took me %d clicks (%f seconds).\n",end,((float)end)/CLOCKS_PER_SEC);
time_spent = (((float)end) * 1000.0 / ((float)CLOCKS_PER_SEC)); // Time using clock BAD
printf("\n TIME %dms|%dms|%fms|%d\n",begin,end, time_spent,CLOCKS_PER_SEC);
}
return 0;
}
But I get 0 clicks all time. I think usleep is not waiting 200 ms exactly, so I need to calculate how much time cost the function to encode audio using ffmpeg with synchronization.
I think the problem is that you're using the clock() function.
The clock function determines the amount of processor time used since the invocation of the calling process, measured in CLOCKS_PER_SEC of a second.
So for example:
clock_t start = clock();
sleep(8);
clock_t finish = clock();
printf("It took %d seconds to execute the for loop.\n",
(finish - start) / CLOCKS_PER_SEC);
This code will give you a value of 0. Because the code was not using the processor, it was sleeping.
This code however:
long i;
clock_t start = clock();
for (i = 0; i < 100000000; ++i)
exp(log((double)i));
clock_t finish = clock();
printf("It took %d seconds to execute the for loop.\n",
(finish - start) / CLOCKS_PER_SEC);
Will give you a count of 8seconds, because the code was using the processor the whole time.

C - measuring computing time

is there any simple way how to measure computing time in C? I tried time utility when executed, but I need to measure specific part of a program.
Thanks
You can use the clock function in <time.h> along with the macro CLOCKS_PER_SEC:
clock_t start = clock() ;
do_some_work() ;
clock_t end = clock() ;
double elapsed_time = (end-start)/(double)CLOCKS_PER_SEC ;
Now elapsed_time holds the time it took to call do_some_work, in fractional seconds.
You can try the profiler "gprof". More information here: http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html
You can generally use the clock() function to get the start and end times of a single call to your function being tested. If, however, do_some_work() is particularly fast, it needs to be put in a loop and have the cost of the loop itself factored out, something like:
#define COUNT 10000
// Get cost of naked loop.
clock_t start_base = clock();
for (int i = count; i > 0; i--)
;
clock_t end_base = clock();
// Get cost of loop plus work.
clock_t start = clock();
for (int i = count; i > 0; i--)
do_some_work() ;
clock_t end = clock();
// Calculate cost of single call.
double elapsed_time = end - start - (end_base - start_base);
elapsed_time = elapsed_time / CLOCKS_PER_SEC / COUNT;
This has at least two advantages:
you'll get an average time which is more representative of the actual time it should take; and
you'll get a more accurate answer in the case where the clock() function has a limited resolution.
#codebolt - Thank you! very nice. On Mac OS X, I added an include of time.h, and pasted in your four lines. Then I printed the values of start, stop (integers) and elapsed time. 1mS resolution.
output:
3 X: strcpy .name, .numDocks: start 0x5dc end 0x5e1 elapsed: 0.000005
calloc: start 0x622 end 0x630 elapsed: 0.000014
in my foo.c program I have
#include <libc.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
but it works without explicitly including time.h. One of the others must bring it in.
Actual code:
clock_t start = clock() ;
strcpy( yard2.name, temp ); /* temp is only persistant in main... */
strcpy( yard1.name, "Yard 1");
strcpy( yard3.name, "3 y 3 a 3 r 3 d 3");
yard1.numDocks = MAX_DOCKS; /* or so I guess.. */
yard2.numDocks = MAX_DOCKS; /* or so I guess.. */
yard3.numDocks = MAX_DOCKS; /* or so I guess.. */
clock_t end = clock() ;
double elapsed_time = (end-start)/(double)CLOCKS_PER_SEC ;
printf("3 X: strcpy .name, .numDocks: start 0x%x end 0x%x elapsed: %-12:8f \n", start, end, elapsed_time );
start = clock() ;
arrayD = calloc( yard2.numDocks, sizeof( struct dock ) ); /* get some memory, init it to 0 */
end = clock() ;
elapsed_time = (end-start)/(double)CLOCKS_PER_SEC ;
printf("calloc: start 0x%x end 0x%x elapsed: %-12:8f \n", start, end, elapsed_time );

Resources