The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on April 28, 2006, 11:21:42 AM

Title: Code alignment test
Post by: hutch-- on April 28, 2006, 11:21:42 AM
If anyone has the time, could you build and run this test, its designed to test speed differences between aligned and unaligned labels at the start of an intensive loop.

These are the times I typically get with it. I would post the complete test piece but with the zero padding, it builds at 266k which is a bit big to post.


297 misaligned
297 aligned
609 misaligned
297 aligned
297 misaligned
297 aligned
297 misaligned
297 aligned
610 misaligned
297 aligned
312 misaligned
297 aligned
297 misaligned
297 aligned
297 misaligned
297 aligned
-----------------------------
377 misaligned average
297 aligned average
-----------------------------
Press any key to continue ...





; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

align 16

main proc

    LOCAL lcnt  :DWORD
    LOCAL mcnt  :DWORD      ; misaligned count
    LOCAL acnt  :DWORD      ; aligned count

    mov acnt, 0             ; zero both counters
    mov mcnt, 0

    invoke SetPriorityClass,FUNC(GetCurrentProcess),REALTIME_PRIORITY_CLASS

    push esi

    invoke SleepEx,1,0

    REPEAT 8

  ; ********************************************

    invoke GetTickCount
    push eax
    mov esi, 400000000
    jmp @F

  REPEAT 2048
    db 0,0,0,0,0,0,0,0  ; 16k inline padding
  ENDM

  align 4
  nop
  nop
  nop
  nop
  nop
  nop
  nop       ;;; 13 nops
  nop
  nop
  nop
  nop
  nop
  nop
  ; ---------------------------
  @@:
    mov lcnt, 0     ; short intensive loop
    sub esi, 1
    jnz @B
  ; ---------------------------
    invoke GetTickCount
    pop ecx
    sub eax, ecx
    add mcnt, eax
    print str$(eax)," misaligned",13,10

    invoke SleepEx,1,0

  ; ********************************************

    invoke GetTickCount
    push eax
    mov esi, 400000000
    jmp @F

  REPEAT 2048
    db 0,0,0,0,0,0,0,0  ; 16k inline padding
  ENDM

  align 4
  ; ---------------------------
  @@:
    mov lcnt, 0     ; short intensive loop
    sub esi, 1
    jnz @B
  ; ---------------------------
    invoke GetTickCount
    pop ecx
    sub eax, ecx
    add acnt, eax
    print str$(eax)," aligned",13,10

    invoke SleepEx,1,0

  ; ********************************************

    ENDM

    pop esi

    print "-----------------------------",13,10
    shr mcnt, 3
    print str$(mcnt)," misaligned average",13,10
    shr acnt, 3
    print str$(acnt)," aligned average",13,10
    print "-----------------------------",13,10

    invoke SetPriorityClass,FUNC(GetCurrentProcess),NORMAL_PRIORITY_CLASS

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Title: Re: Code alignment test
Post by: Ghirai on April 28, 2006, 12:08:25 PM
Tested on AMD Athlon 64 3000+:

Quote656 misaligned
671 aligned
656 misaligned
656 aligned
657 misaligned
438 aligned
438 misaligned
437 aligned
657 misaligned
656 aligned
656 misaligned
672 aligned
657 misaligned
438 aligned
438 misaligned
437 aligned
-----------------------------
601 misaligned average
550 aligned average
-----------------------------

I removed 'inkey', it wouldn't let me compile.
Title: Re: Code alignment test
Post by: dsouza123 on April 28, 2006, 12:48:28 PM

961 misaligned
921 aligned
922 misaligned
921 aligned
921 misaligned
611 aligned
611 misaligned
611 aligned
921 misaligned
921 aligned
921 misaligned
922 aligned
921 misaligned
611 aligned
611 misaligned
611 aligned
-----------------------------
848 misaligned average
766 aligned average
-----------------------------
Press any key to continue ...


Athlon Thunderbird 1300 Mhz
Windows XP
Title: Re: Code alignment test
Post by: Roger on April 28, 2006, 01:07:32 PM
Hutch,

I ran it several times each gave the same result within 2% -3%

Processor used Pentium IV 2.28GHZ clock
OS WinXP  SP2

I tried it on my 486 with win95 and the system locked up.

:boohoo:
328 misaligned
329 aligned
656 misaligned
328 aligned
344 misaligned
328 aligned
328 misaligned
328 aligned
657 misaligned
344 aligned
328 misaligned
329 aligned
328 misaligned
344 aligned
328 misaligned
328 aligned
-----------------------------
412 misaligned average
332 aligned average
-----------------------------
Press any key to continue ...


Could you expound on your ideas here?

Regards Roger
Title: Re: Code alignment test
Post by: hutch-- on April 28, 2006, 01:29:25 PM
Roger,

What I have tried to do is isolate the external conditions that effect a timing on something as obscure as aligning a label. The SleepEx() OS yield is so each return gets the identical processor priority and the 16k of zero padding is so the code cache is broken for each looped iteration. The PIV I use is reasonably insensitive to code alignment but some of the old timers I have worked on responded a lot more to aligning code labels that were hit in loop code at high speed.

I think the later AMD hardware is more sensitive to code alignment as well but I have not yet run it on my Sempron 2.4.
Title: Re: Code alignment test
Post by: Roger on April 28, 2006, 01:36:25 PM
Hutch,

Sorry I was too impatient, it didn't lock up results were:-

52123 misaligned
55560 aligned
52049 misaligned
52322 aligned
52100 misaligned
62318 aligned
51812 misaligned
51802 aligned
51929 misaligned
52127 aligned
51810 misaligned
57605 aligned
51902 misaligned
51794 aligned
51788 misaligned
51790 aligned
-------------
51939 misaligned
54414 aligned
---------------

I can run it again but it will have to be overnight as a batch job

Regards Roger4
Title: Re: Code alignment test
Post by: hutch-- on April 28, 2006, 01:38:13 PM
Roger,

Just drop the loop count down as its far too high for a 486, You only need about 500 ms or a bit longer to get consistent results.
Title: Re: Code alignment test
Post by: Roger on April 28, 2006, 01:40:00 PM
Hutch,

What and spoil the joke!

Roger
Title: Re: Code alignment test
Post by: P1 on April 28, 2006, 02:40:49 PM
PIV 2GHz on W2K SP4
501 misaligned
401 aligned
400 misaligned
401 aligned
400 misaligned
401 aligned
441 misaligned
420 aligned
401 misaligned
400 aligned
401 misaligned
401 aligned
400 misaligned
411 aligned
400 misaligned
451 aligned
-----------------------------
418 misaligned average
410 aligned average
-----------------------------
Press any key to continue ...

Regards,  P1  :8)
Title: Re: Code alignment test
Post by: jdoe on April 28, 2006, 03:41:45 PM
P4 1.68GHz
XP SP2


922 misaligned
480 aligned
511 misaligned
481 aligned
471 misaligned
470 aligned
471 misaligned
471 aligned
480 misaligned
551 aligned
541 misaligned
471 aligned
470 misaligned
481 aligned
471 misaligned
490 aligned
-----------------------------
542 misaligned average
486 aligned average
-----------------------------
Press any key to continue ...


Title: Re: Code alignment test
Post by: MichaelW on April 28, 2006, 11:21:53 PM
P3-500
W2K SP4
With a bunch of stuff running

2384 misaligned
2383 aligned
2383 misaligned
2384 aligned
2383 misaligned
1593 aligned
1592 misaligned
1592 aligned
2384 misaligned
2393 aligned
2384 misaligned
2383 aligned
2384 misaligned
1602 aligned
1592 misaligned
1592 aligned
-----------------------------
2185 misaligned average
1990 aligned average
-----------------------------

Title: Re: Code alignment test
Post by: Jimg on April 29, 2006, 03:45:11 AM
This is very strange.  I have run this many times, and the 5th and 6th aligned tests always blow up the aligned average.

Toshiba laptop, XP home, Celeron 2.0GHz, 496 MB ram.


422 misaligned
407 aligned
391 misaligned
391 aligned
391 misaligned
453 aligned
407 misaligned
391 aligned
390 misaligned
750 aligned
500 misaligned
781 aligned
422 misaligned
516 aligned
422 misaligned
391 aligned
-----------------------------
418 misaligned average
510 aligned average
-----------------------------
Press any key to continue ...

Edit--
Oops- was in debug mode.   Still strange timings in release mode-
422 misaligned
390 aligned
390 misaligned
391 aligned
391 misaligned
421 aligned
422 misaligned
390 aligned
406 misaligned
750 aligned
391 misaligned
781 aligned
407 misaligned
532 aligned
406 misaligned
406 aligned
-----------------------------
404 misaligned average
507 aligned average
-----------------------------
Press any key to continue ...

[attachment deleted by admin]
Title: Re: Code alignment test
Post by: PBrennick on April 29, 2006, 04:28:39 AM
Windows XP on a SONY laptop using an AMD Athlon 1.00GHz, 256 MB RAM.

1442 misaligned
1242 aligned
1201 misaligned
1272 aligned
1202 misaligned
911 aligned
802 misaligned
871 aligned
1202 misaligned
1321 aligned
1202 misaligned
1422 aligned
1202 misaligned
921 aligned
802 misaligned
801 aligned
-----------------------------
1131 misaligned average
1095 aligned average
-----------------------------
Press any key to continue ...

Paul
Title: Re: Code alignment test
Post by: Roger on April 29, 2006, 01:41:52 PM
Hutch,

I ran your test again on both machines and noticed funny results. I had assembled a second version without the Inkey instruction so it could be used in a .BAT file without needing keyboard input. On testing it I found that it gives consistently (20 -30 runs) different results.

With inkey

328 misaligned
328 aligned
656 misaligned
329 aligned
328 misaligned
328 aligned
328 misaligned
329 aligned
656 misaligned
375 aligned
328 misaligned
344 aligned
328 misaligned
343 aligned
328 misaligned
328 aligned
-----------------------------
410 misaligned average
338 aligned average
-----------------------------
Press any key to continue ...


Without inkey

328 misaligned
640 aligned
328 misaligned
328 aligned
328 misaligned
344 aligned
329 misaligned
328 aligned
328 misaligned
328 aligned
657 misaligned
328 aligned
328 misaligned
328 aligned
328 misaligned
328 aligned
-----------------------------
369 misaligned average
369 aligned average
-----------------------------


I see that the times are consistently 328 sometimes 329 except:-
With inkey;    The 3rd and 9th times are twice this at about 656 (ocasionally as low as 640) and the 14th time is often about 340
Without inkey;  It is the 2nd and 11th times which are twice the length and there are more slightly longer times, ~340, but I see less pattern in them.

You will appreciate that as the odd times are 'misaligned' and the even times are 'aligned' the difference in average times is totally dependent on when the 'long' times occur.

I am sure that Inkey is having some effect but I don't know what it might be. As it is executed at the end, and I assume its effects do not occur before the cause, I suppose it must be due to the space it takes in the executable file.

I did a file compare on the 2 .asm files just to make sure that the only differece was inkey.

On the 486 the max difference in times was only 4% but there was a pattern in that the 5th was always 3%-4% less than the 4th.


Hope this is of help, Regards Roger

Title: Re: Code alignment test
Post by: tofu-sensei on April 29, 2006, 03:52:40 PM
594 misaligned
594 aligned
593 misaligned
594 aligned
594 misaligned
390 aligned
391 misaligned
391 aligned
594 misaligned
594 aligned
594 misaligned
593 aligned
594 misaligned
391 aligned
390 misaligned
390 aligned
-----------------------------
543 misaligned average
492 aligned average
-----------------------------
Title: Re: Code alignment test
Post by: asmfan on April 29, 2006, 03:57:11 PM
I think task switching plays its role... time to time;)
Title: Re: Code alignment test
Post by: Mark Jones on April 29, 2006, 04:36:02 PM
Oooh, just thought of a cool idea. Make a browser plugin which adds a new right-click command to selected text: Assemble Text. :) That would make testing stuff here very fast and easy.
Title: Re: Code alignment test
Post by: PBrennick on April 29, 2006, 05:29:53 PM
Mark,
It seems we two are always in concert, you come up the an idea and I expound on it.  We ought to at least 'date'.  :bg

Anyway, an easier way to do what you are thinking about (a very good idea, BTW) is to use WBrowser and just code it into the source which is available.

Paul
Title: Re: Code alignment test
Post by: Human on April 29, 2006, 07:39:36 PM
well alignment on new cpus isnt possible, i know it by testing by jpg decompressor on my xp1700+
i optimize code remove few instructions and its slower due align of rest changed. i tried to force some places with adding align but it doesnt help in most cases. its like random. also with testing loops you should always skip 1st, due there is too big difference, code isnt cached yet and its big slowdown, counting time and ticks from second gives more stable results. offcourse to test we need realtime priority kill all other task and console has to be fullscreen in text mode, ticks and ms are lower. and also stable, due windows takes too much time to render text on screen.
Title: Re: Code alignment test
Post by: PBrennick on April 29, 2006, 09:03:19 PM
Human,
I think you may be right 'and' wrong.  Remember, please, that if you optimize the code and then remove a few instructions?

Paul
Title: Re: Code alignment test
Post by: Mark Jones on April 29, 2006, 09:28:28 PM
Sorry Paul, but I'm already taken. :toothy
Title: Re: Code alignment test
Post by: zooba on April 29, 2006, 10:19:12 PM
Quote from: Mark Jones on April 29, 2006, 04:36:02 PM
Oooh, just thought of a cool idea. Make a browser plugin which adds a new right-click command to selected text: Assemble Text. :) That would make testing stuff here very fast and easy.

It's quite easy to do, as long at Javascript/VBScript settings will let you access the file system. You make an HTML file with a script in it, place a registry key in HKCU\Software\Microsoft\Internet Explorer\MenuExt\<plugin name here> and set the default to the HTML file.

If I get a chance I might have a play with it :U

Cheers,

Zooba
Title: Re: Code alignment test
Post by: GregL on May 03, 2006, 06:44:01 PM
PIII 1GHz
XP Pro SP2

1202 misaligned
1201 aligned
1202 misaligned
1202 aligned
1201 misaligned
801 aligned
801 misaligned
801 aligned
1202 misaligned
1201 aligned
1202 misaligned
1202 aligned
1202 misaligned
801 aligned
801 misaligned
801 aligned
-----------------------------
1101 misaligned average
1001 aligned average
-----------------------------

Title: Re: Code alignment test
Post by: P1 on May 03, 2006, 06:56:21 PM
Quote from: Mark Jones on April 29, 2006, 04:36:02 PM
Oooh, just thought of a cool idea. Make a browser plugin which adds a new right-click command to selected text: Assemble Text. :) That would make testing stuff here very fast and easy.
That would be great, but I have not seen, code here complete enough, for that to be useful.

Regards,  P1  :8)