Which is faster?

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Which is faster?

Post 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))
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

I think, is the same, but the first one is more readable :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
berryoxide
Enthusiast
Enthusiast
Posts: 136
Joined: Fri Aug 15, 2008 6:04 pm
Location: Debugger

Post 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...
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post 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
I love Purebasic.
sampb
User
User
Posts: 51
Joined: Tue Feb 12, 2008 8:08 am

Post by sampb »

Hi all,

How to get "ASM output"?

Thanks in advance
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 ;)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

sampb wrote:Hi all,

How to get "ASM output"?

Thanks in advance
pbcompiler.exe yourfile.pb /COMMENTED
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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 :)
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

"hi-perf" as in what type of high performance procedure you talking about?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply