17 September 2015, 09:49 | #1 |
Registered User
Join Date: Aug 2013
Location: france
Posts: 20
|
How to test time for piece of code
Hi everybody,
today I decidedly full of questions. how to test the time it takes a piece of code. I never used timers on amiga. Now I change the color 0 of the copperliste but it is not very accurate. thank you. Powergoo |
17 September 2015, 22:50 | #2 | |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
|
Quote:
When you have a frame-based program (usually a game), then modifying a color is definitely a good method to see how much time it needs from the available frame length. It is useless when you're interested in the Milliseconds or CPU cycles. |
|
18 September 2015, 07:57 | #3 |
Registered User
Join Date: Aug 2013
Location: france
Posts: 20
|
in fact I wish I could do:
value1 = actual_precise_timer instructions instructions instructions instructions ... value2 = actual_precise_timer timeofcode = value2 - value1 there are times to no copperliste yes i'm interested in the Milliseconds or CPU cycles. |
19 September 2015, 20:29 | #4 |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
|
Untested, but try something like this:
Code:
CIAB equ $bfd000 CIATALO equ $400 CIATAHI equ $500 CIACRA equ $e00 ; clear and start Timer A lea CIAB,a0 moveq #0,d0 move.b d0,CIATALO(a0) move.b d0,CIATAHI(a0) ; force load, one-shot mode, start timer move.b #%00011001,CIACRA(a0) ; ... do something ... ; stop timer and read result lea CIAB,a0 move.b #$00,CIACRA(a0) moveq #0,d0 sub.b CIATAHI(a0),d0 lsl.w #8,d0 sub.b CIATALO(a0),d0 ; d0: time in 02 clocks I have set the timer to one-shot mode, so it stops when it underflows. Then you have a chance to detect such a case. When the code to measure requires more time than 65536 02-clocks (ca. 92ms) you can add Timer B and let it count underflows of Timer A. You would set bit 6 of CIACRB for that. The CIA timers are counting backwards, so the result is subtracted from zero. To get the time in milliseconds or microseconds you need to know that the timer runs with 709379Hz on a PAL and with 715909Hz on an NTSC system. If you want to be very precise you have to subtract the time for Code:
lea CIAB,a0 move.b #$00,CIACRA(a0) |
23 September 2015, 09:03 | #5 |
Registered User
Join Date: Aug 2013
Location: france
Posts: 20
|
Thanks you phx, I try this code this night
|
24 September 2015, 15:31 | #6 |
Registered User
Join Date: Aug 2013
Location: france
Posts: 20
|
phx I tried your code but I still get 0 in the register d0
|
24 September 2015, 20:58 | #7 |
Registered User
Join Date: Aug 2013
Location: france
Posts: 20
|
I feel that we must initialize the timer with a value other than 0.
I modified like this and I think it works ( d0 increase now ) In any case it's going to allow me to test whether a routine is faster than another. If you see any problems do not hesitate to tell me because I have not really understand the 8520. Thanks you phx for your help ! Code:
CIAB equ $bfd000 CIATALO equ $400 CIATAHI equ $500 CIACRA equ $e00 ; clear and start Timer A lea CIAB,a0 moveq #-1,d0 move.b d0,CIATALO(a0) move.b d0,CIATAHI(a0) ; force load, one-shot mode, start timer move.b #%00011001,CIACRA(a0) ; ... do something ... nop nop nop nop ; stop timer and read result lea CIAB,a0 move.b #$00,CIACRA(a0) move.b CIATAHI(a0),d1 move.b CIATALO(a0),d2 moveq #-1,d0 lsl.w #8,d1 move.b d2,d1 sub.w d1,d0 |
12 October 2015, 15:39 | #8 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Sorry to wake you up if your problem is solved already
Using CIA timers is perhaps a little bit overkill for that kind of use. Personnally I start a vblank counter with AddIntServer() and run a loop containing the code i wish to test so that it takes a few seconds. |
13 October 2015, 22:00 | #9 |
AMOS Extensions Developer
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 44
Posts: 1,924
|
So you're just trying to see how many times that code executes within a few seconds??
|
14 October 2015, 00:41 | #10 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,764
|
That works perfectly fine. Or you could execute code an appropriate number of times and count how many frames it takes. On my 50 mhz 68030 I usually execute code snippets one million times and count the number of frames. I also turn off the system so that the OS doesn't affect the measurements.
|
14 October 2015, 01:12 | #11 | |
Code Kitten
Join Date: Aug 2015
Location: Montreal/Canadia
Age: 52
Posts: 1,178
|
Quote:
However if you want to profile specific pieces of code inside a running program, just like a c/c++ compiler would for each function then you are better off with using timers. This said, if you want to reduce overhead you could simply use the VPOS and HPOS registers. Keep a Vblank counter, then each time you want to store the current time, just dump VHPOSR, VPOSR, vblank counter (in this order) somewhere. You can then easily reconstruct the corresponding time later. You probably will have to take into account that VPOS can increase between the moment you store HPOS and the moment you store VPOS but this is easily detected. The advantage of this method is that it is quite precise and requires very simple code to dump the current time to memory and thus has a small overhead. |
|
14 October 2015, 12:30 | #12 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Using the timers is not overkill if you want accuracte measurements, but if you care that much about accuracy then you can look up the cycle counts in the M68K User's Manual, then you'll know exactly how much time a piece of code will need.
But IMO skip the timers and the interrupts, and definitely skip membership in the "Cycle Counter Party", and just run your code multiple times and use the TOD clocks instead. You'll never have an error greater than about 64 µs, which means your result will be accurate within 0.01% even if you only measure over a single second. EDIT: Example. Divide the result by the horizontal scan frequency if you want the time in seconds: Code:
(make sure the system interrupts are disabled while you run this) moveq #0, d1 move.b CIABTODHI, d1 swap d1 move.b CIABTODMID, d1 lsl.w #8, d1 move.b CIABTODLO, d1 (your code here) moveq #0, d0 move.b CIABTODHI, d0 swap d0 move.b CIABTODMID, d0 lsl.w #8, d0 move.b CIABTODLO, d0 sub.l d1, d0 bpl .1 add.l #1<<24, d0 .1 (result in D0) Last edited by Leffmann; 14 October 2015 at 12:43. |
14 October 2015, 14:47 | #13 | |
Code Kitten
Join Date: Aug 2015
Location: Montreal/Canadia
Age: 52
Posts: 1,178
|
Quote:
I guess which of these methods to use really boils down to what the end usage is: - If you want to profile many functions in an existing interactive program: use VHPOSR/VPOSR. - If you want to time a specific function in isolation and does not need ultra precision: use Meynaf's VBL counter. - If you want to time a specific function in isolation and *do* require high precision and do not want to wait: use Leffmann's time of day suggestion. |
|
14 October 2015, 17:22 | #14 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Cycle counts don't always work. For 68000, ok, no problem. But for 68020+, when cache misses and memory accesses (especially chipmem) come into play, timings become more and more unpredictable.
|
15 October 2015, 11:37 | #15 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
|
but this will affect measurements using a timer as well, and the only solution to that is taking the average of many iterations.
|
17 October 2015, 11:53 | #16 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
|
17 October 2015, 23:08 | #17 |
AMOS Extensions Developer
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 44
Posts: 1,924
|
Thorham and ReadOnlyCat,
Thanks for answering my question and confirming my suspicions |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
The greatest code editor feature of all time | Photon | Coders. General | 113 | 25 September 2022 11:12 |
non-system startup code time/clock problem | modrobert | Coders. Asm / Hardware | 89 | 14 December 2018 15:37 |
ALWAYS test your code on real hardware!! | h0ffman | Coders. General | 32 | 16 July 2015 21:02 |
Gadgets Lost in Time 1.0 Uploaded to the Zone (Please Test and Provide Feedback) | Abaddon | project.WHDLoad | 0 | 19 April 2014 01:43 |
Time Soldier v1.3 Uploaded to the Zone (Please Test and Provide Feedback) | Abaddon | project.WHDLoad | 0 | 17 December 2010 19:33 |
|
|