How can I measure the execution time of a function?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I measure the execution time of a function?

Jump to solution
3,560 Views
julianhoerz
Contributor II

The command "clock()" doesn't work correct for me, also the command "time()".

It always returns the 2^32-1.

My code looks like this:

clock_t begin = clock();

my_algo();

clock_t end = clock();

Normally the values of the variables should be different but in both variables (begin and end) are the same numbers... Always 4294967295 (2^32-1).

I am using a LPC4357 with LPCXpresso 8.1.4.

 

The clock-function comes from time.h from redlib....

 

I cannot use the RTC example from LPC Open, because it can only count seconds....

Labels (2)
Tags (1)
0 Kudos
1 Solution
2,539 Views
thefallguy
Contributor IV

Have you read this thread?

time () does nothing | www.LPCware.com

i.e you  have to implement __sys_time() yourself as the library has no idea of your hardware. It has to run on any cortex microcontoller.

View solution in original post

0 Kudos
5 Replies
2,539 Views
martinlorenz
Contributor II

You can count the number of cpu cycles or instructions by using the Data Watchpoint and Tracing feature (DWT) of ARM.

Example code tested on LPC1549:

  // enable the use DWT

    *DEMCR = *DEMCR | 0x01000000;

    // Reset cycle counter

    *DWT_CYCCNT = 0;

    // enable cycle counter

    *DWT_CONTROL = *DWT_CONTROL | 1 ;

  uint32_t cpuCycles =  *DWT_CYCCNT;;

/* Do something */

    cpuCycles = *DWT_CYCCNT-cpuCycles;

    float time=1.0*cpuCycles/Chip_Clock_GetSystemClockRate();

0 Kudos
2,539 Views
berndsirozynski
Contributor III

Hello,

To measure the executiotime of my functions, i set a portbit at the begin and clear it at the end of the function.

With an Oscilloscope i can read the Pulsewidth.

Know, that the times differ to the Compiler Optimization settings.

0 Kudos
2,539 Views
julianhoerz
Contributor II

Ok, thank you for your help! You're right!

I had to make it via semihosting: WhatIsSemiHosting - ** Code Red Support Site **

I linked the main application against semihosting in the project properties and enabled semihosting in the board.h

Here did I find the solution: Using printf()

Thank you! :smileyhappy:

0 Kudos
2,539 Views
julianhoerz
Contributor II

Hi,

thank you for your reply.

Yes, I saw this thread and I'll read it again now.

I'm running my code on a cortex m4. I have the LPC-Link2 for debugging.

I'll also look it up whether I have to implement __sys_time() anywhere.

I'll let you know the result :smileyhappy:

0 Kudos
2,540 Views
thefallguy
Contributor IV

Have you read this thread?

time () does nothing | www.LPCware.com

i.e you  have to implement __sys_time() yourself as the library has no idea of your hardware. It has to run on any cortex microcontoller.

0 Kudos