Page 1 of 1

Which is faster?

Posted: Sat Aug 30, 2008 11:25 pm
by SFSxOI
Which one of these methods is faster? Both accomplish the same thing:

Code: Select all

rslt_test_a.l = Random(256) 
test_ret_a$ = Hex(rslt_test_a)

or......

test_ret_a$ = Hex(Random(256))

Posted: Sat Aug 30, 2008 11:36 pm
by ts-soft
I think, is the same, but the first one is more readable :wink:

Posted: Sat Aug 30, 2008 11:42 pm
by berryoxide
Look at the ASM output and figure out if there are different calls used and count the CPU cycles together if you need :)
In practice both do the same thing maybe even equally fast, but in theory depending on the compiler and how it parses the source code, there can be a slight 0.1% speed difference that doesn't really count in overall performance...

Posted: Sun Aug 31, 2008 7:00 am
by Heathen
#1

Code: Select all

; rslt_test_a.l = Random(256)
  MOV    eax,256
  CALL   PB_Random
  MOV    dword [v_rslt_test_a],eax
; test_ret_a$ = Hex(rslt_test_a)
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  PUSH   dword [v_rslt_test_a]
  CALL  _PB_Hex@8
  LEA    ecx,[v_test_ret_a$]
  POP    edx
  CALL   SYS_AllocateString
#2 (should be slightly faster)

Code: Select all

; test_ret_a$ = Hex(Random(256))
  MOV    eax,[_PB_StringBasePosition]
  PUSH   eax
  PUSH   eax
  MOV    eax,256
  CALL   PB_Random
  PUSH   eax
  CALL  _PB_Hex@8
  LEA    ecx,[v_test_ret_a$]
  POP    edx
  CALL   SYS_AllocateString

Posted: Sun Aug 31, 2008 3:57 pm
by sampb
Hi all,

How to get "ASM output"?

Thanks in advance

Posted: Sun Aug 31, 2008 6:37 pm
by Psychophanta
In opposit to ts-soft, sorry but i think the 2nd one is much more readable :o
sampb wrote:Hi all,

How to get "ASM output"?

Thanks in advance
look at the manual, at the GetDisASMString() function ;)

Posted: Sun Aug 31, 2008 6:39 pm
by Trond
sampb wrote:Hi all,

How to get "ASM output"?

Thanks in advance
pbcompiler.exe yourfile.pb /COMMENTED

Posted: Sun Aug 31, 2008 7:58 pm
by Kaeru Gaman
the performance is not the only thing in question.
normally, you would use a random number not only for output, but also for calculations.
the first version already holds the value in a variable, the second one only provides the string.

Posted: Sun Aug 31, 2008 8:07 pm
by SFSxOI
well, after checking it out some more and considering the input from you folks i've decided to go with the second one and kind of combine all your words of wisdom into one solution. I only really need the string, it helps 'neaten' the procedure it lives in by reducing clutter in the procedure and makes the overall procedure more readable, and the time differences are not that great between the two enough to really matter. Thanks for your input folks :)

Posted: Mon Sep 01, 2008 12:05 am
by pdwyer
If this is such a tight loop that a difference like this is being sought for perf reasons, you many want to consider a faster random() proc. Not criticising PB's but it's chosen as a good balance of speed and randomness. Just like you wouldn't use it in a security application perhaps you should replace it for a hi-perf one. It's a black box after all.

Depends on what you need

Posted: Mon Sep 01, 2008 1:17 pm
by SFSxOI
"hi-perf" as in what type of high performance procedure you talking about?

Posted: Mon Sep 01, 2008 2:24 pm
by pdwyer
There are many different PRNG's, some optimised for randomness benchmarks, some for speed, some for security. Others for all round

http://en.wikipedia.org/wiki/PRNG

I believe a couple are implemented in the tips section already.

Sounds like, from the question, that you are after one optimised for speed.