English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 17 September 2015, 09:49   #1
Powergoo
Registered User
Powergoo's Avatar
 
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
Powergoo is offline  
Old 17 September 2015, 22:50   #2
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,604
Quote:
Originally Posted by Powergoo View Post
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.
Why? Which kind of accuracy do you need?

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.
phx is offline  
Old 18 September 2015, 07:57   #3
Powergoo
Registered User
Powergoo's Avatar
 
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.
Powergoo is offline  
Old 19 September 2015, 20:29   #4
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,604
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 chose CIA-B Timer A as the CIA-B timers are not used by the operating system. But note that other programs may use them, like for example a protracker playing routine.

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)
from the result.
phx is offline  
Old 23 September 2015, 09:03   #5
Powergoo
Registered User
Powergoo's Avatar
 
Join Date: Aug 2013
Location: france
Posts: 20
Thanks you phx, I try this code this night
Powergoo is offline  
Old 24 September 2015, 15:31   #6
Powergoo
Registered User
Powergoo's Avatar
 
Join Date: Aug 2013
Location: france
Posts: 20
phx I tried your code but I still get 0 in the register d0
Powergoo is offline  
Old 24 September 2015, 20:58   #7
Powergoo
Registered User
Powergoo's Avatar
 
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
Powergoo is offline  
Old 12 October 2015, 15:39   #8
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 47
Posts: 3,702
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.
meynaf is offline  
Old 13 October 2015, 22:00   #9
Lonewolf10
AMOS Extensions Developer
Lonewolf10's Avatar
 
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 40
Posts: 1,923
Quote:
Originally Posted by meynaf View Post
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.
So you're just trying to see how many times that code executes within a few seconds??
Lonewolf10 is offline  
Old 14 October 2015, 00:41   #10
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 43
Posts: 3,088
Quote:
Originally Posted by Lonewolf10 View Post
So you're just trying to see how many times that code executes within a few seconds??
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.
Thorham is offline  
Old 14 October 2015, 01:12   #11
ReadOnlyCat
Code Kitten

 
Join Date: Aug 2015
Location: Montreal/Canadia
Age: 48
Posts: 1,143
Quote:
Originally Posted by Lonewolf10 View Post
So you're just trying to see how many times that code executes within a few seconds??
Yes. If it's code that you can easily isolate then Meynaf's method wil give you relatively precise measurements because of the large number of loops.

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.
ReadOnlyCat is offline  
Old 14 October 2015, 12:30   #12
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,265
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.
Leffmann is offline  
Old 14 October 2015, 14:47   #13
ReadOnlyCat
Code Kitten

 
Join Date: Aug 2015
Location: Montreal/Canadia
Age: 48
Posts: 1,143
Quote:
Originally Posted by Leffmann View Post
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)
    [...]
.1  (result in D0)
Nice. This has about the same overhead as dumping VHPOSR and VPOSR and requires a simpler conversion but is less precise (for one measurement).

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.

ReadOnlyCat is offline  
Old 14 October 2015, 17:22   #14
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 47
Posts: 3,702
Quote:
Originally Posted by Leffmann View Post
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.
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.
meynaf is offline  
Old 15 October 2015, 11:37   #15
hooverphonique
ex. demoscener "Bigmama"

 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,062
Quote:
Originally Posted by meynaf View Post
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.
but this will affect measurements using a timer as well, and the only solution to that is taking the average of many iterations.
hooverphonique is offline  
Old 17 October 2015, 11:53   #16
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 47
Posts: 3,702
Quote:
Originally Posted by hooverphonique View Post
but this will affect measurements using a timer as well, and the only solution to that is taking the average of many iterations.
... which is exactly what i'm doing with my vbl trick. For very short code i could execute it up to 50,000,000 times.
meynaf is offline  
Old 17 October 2015, 23:08   #17
Lonewolf10
AMOS Extensions Developer
Lonewolf10's Avatar
 
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 40
Posts: 1,923
Thorham and ReadOnlyCat,

Thanks for answering my question and confirming my suspicions
Lonewolf10 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
non-system startup code time/clock problem modrobert Coders. Asm / Hardware 89 14 December 2018 15:37
The greatest code editor feature of all time Photon Coders. General 102 07 December 2016 21:27
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:41.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.08618 seconds with 15 queries