OMRON Forums

# How to calculate the execution time of a C application or program?

## Recommended Posts

The following code is an example on how to use the fclock() function to calculate the execution time of a C application. [code] #include #include // Global Rt/Gp Shared memory pointers //------------------------------------------------------------- // The following is a projpp created file from the User defines //------------------------------------------------------------- #include "../../Include/pp_proj.h" int main(void) { SHM *pSHM; // defining a pointer to the shared memroy location // using this pointer you can access most of the structures InitLibrary(); // Initializing the Library pSHM = GetSharedMemPtr(); // Initializing the pointer to the shared memory int d1,d2,d3,d4,d5,d6,d7,d8,d9; unsigned long long start = fclock(); // Save the start fclock() value in an unsigned long long variable // The following code is a brute-force solution for the following math question : // A curious mathematician noticed that his nine-digit Swiss bank account number used all of the digits 1 through 9 exactly once. Furthermore, the first two digits formed a number divisible by two, the first three digits were a number divisible by three, and so on. // Unfortunately, a few years later when needed to access his account- which is available only to someone who knows the number- he counldn't remember the number nor find the paper on which he had written the number. All he could remember were the relationships described above. Can you help him find his account number? for (d1=1;d1<10;d1++) for(d2=1;d2<10;d2++) if((d1*10+d2)%2==0 && d1!=d2) for(d3=1;d3<10;d3++) if((d1*100+d2*10+d3)%3==0 && d1!=d3 && d2!=d3) for(d4=1;d4<10;d4++) if((d1*1000+d2*100+d3*10+d4)%4==0 && d1!=d4 && d2!=d4 && d3!=d4) for(d5=1;d5<10;d5++) if((d1*10000+d2*1000+d3*100+d4*10+d5)%5==0 && d1!=d5 && d2!=d5 && d3!=d5 && d4!=d5) for(d6=1;d6<10;d6++) if((d1*100000+d2*10000+d3*1000+d4*100+d5*10+d6)%6==0 && d1!=d6 && d2!=d6 && d3!=d6 && d4!=d6 && d5!=d6) for(d7=1;d7<10;d7++) if((d1*1000000+d2*100000+d3*10000+d4*1000+d5*100+d6*10+d7)%7==0 && d1!=d7 && d2!=d7 && d3!=d7 && d4!=d7 && d5!=d7 && d6!=d7) for(d8=1;d8<10;d8++) if((d1*10000000+d2*1000000+d3*100000+d4*10000+d5*1000+d6*100+d7*10+d8)%8==0 && d1!=d8 && d2!=d8 && d3!=d8 && d4!=d8 && d5!=d8 && d6!=d8 && d7!=d8) for(d9=1;d9<10;d9++) if((d1*100000000+d2*10000000+d3*1000000+d4*100000+d5*10000+d6*1000+d7*100+d8*10+d9)%9==0 && d1!=d9 && d2!=d9 && d3!=d9 && d4!=d9 && d5!=d9 && d6!=d9 && d7!=d9 && d8!=d9) { printf("Found it! %d%d%d%d%d%d%d%d%d\n\n",d1,d2,d3,d4,d5,d6,d7,d8,d9); printf("Time elapsed: %.15f seconds\n", 1e-6*pSHM->ClockSF*u64btod(fclock()-start)); // Calculate the elapsed time and scale it to Seconds } CloseLibrary(); return 0; } [/code]
##### Share on other sites

• 3 months later...
• Replies 2
• Created

#### Popular Days

[quote='Sina' pid='221' dateline='1256256540'] The following code is an example on how to use the fclock() function to calculate the execution time of a C application. [code] #include #include // Global Rt/Gp Shared memory pointers //------------------------------------------------------------- // The following is a projpp created file from the User defines //------------------------------------------------------------- #include "../../Include/pp_proj.h" int main(void) { SHM *pSHM; // defining a pointer to the shared memroy location // using this pointer you can access most of the structures InitLibrary(); // Initializing the Library pSHM = GetSharedMemPtr(); // Initializing the pointer to the shared memory int d1,d2,d3,d4,d5,d6,d7,d8,d9; unsigned long long start = fclock(); // Save the start fclock() value in an unsigned long long variable // The following code is a brute-force solution for the following math question : // A curious mathematician noticed that his nine-digit Swiss bank account number used all of the digits 1 through 9 exactly once. Furthermore, the first two digits formed a number divisible by two, the first three digits were a number divisible by three, and so on. // Unfortunately, a few years later when needed to access his account- which is available only to someone who knows the number- he counldn't remember the number nor find the paper on which he had written the number. All he could remember were the relationships described above. Can you help him find his account number? for (d1=1;d1<10;d1++) for(d2=1;d2<10;d2++) if((d1*10+d2)%2==0 && d1!=d2) for(d3=1;d3<10;d3++) if((d1*100+d2*10+d3)%3==0 && d1!=d3 && d2!=d3) for(d4=1;d4<10;d4++) if((d1*1000+d2*100+d3*10+d4)%4==0 && d1!=d4 && d2!=d4 && d3!=d4) for(d5=1;d5<10;d5++) if((d1*10000+d2*1000+d3*100+d4*10+d5)%5==0 && d1!=d5 && d2!=d5 && d3!=d5 && d4!=d5) for(d6=1;d6<10;d6++) if((d1*100000+d2*10000+d3*1000+d4*100+d5*10+d6)%6==0 && d1!=d6 && d2!=d6 && d3!=d6 && d4!=d6 && d5!=d6) for(d7=1;d7<10;d7++) if((d1*1000000+d2*100000+d3*10000+d4*1000+d5*100+d6*10+d7)%7==0 && d1!=d7 && d2!=d7 && d3!=d7 && d4!=d7 && d5!=d7 && d6!=d7) for(d8=1;d8<10;d8++) if((d1*10000000+d2*1000000+d3*100000+d4*10000+d5*1000+d6*100+d7*10+d8)%8==0 && d1!=d8 && d2!=d8 && d3!=d8 && d4!=d8 && d5!=d8 && d6!=d8 && d7!=d8) for(d9=1;d9<10;d9++) if((d1*100000000+d2*10000000+d3*1000000+d4*100000+d5*10000+d6*1000+d7*100+d8*10+d9)%9==0 && d1!=d9 && d2!=d9 && d3!=d9 && d4!=d9 && d5!=d9 && d6!=d9 && d7!=d9 && d8!=d9) { printf("Found it! %d%d%d%d%d%d%d%d%d\n\n",d1,d2,d3,d4,d5,d6,d7,d8,d9); printf("Time elapsed: %.15f seconds\n", 1e-6*pSHM->ClockSF*u64btod(fclock()-start)); // Calculate the elapsed time and scale it to Seconds } CloseLibrary(); return 0; } [/code] [/quote]
##### Share on other sites

In firmware 1.0.0.148 and newer there is a new C API function GetCPUClock() which returns the fclock value in usec. This means the above code could be start = GetCPUClock(); printf("Time elapsed: %.15f seconds\n", (GetCPUClock() - start)/1000000); // Calculate the elapsed time and scale it to Seconds