How can I measure the execution time of a function?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How can I measure the execution time of a function?

ソリューションへジャンプ
5,867件の閲覧回数
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....

ラベル(2)
タグ(1)
0 件の賞賛
返信
1 解決策
4,846件の閲覧回数
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 件の賞賛
返信
5 返答(返信)
4,846件の閲覧回数
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 件の賞賛
返信
4,846件の閲覧回数
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 件の賞賛
返信
4,846件の閲覧回数
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 件の賞賛
返信
4,846件の閲覧回数
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 件の賞賛
返信
4,847件の閲覧回数
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 件の賞賛
返信