Math error using Macro inline without outer ()'s

Just starting out? Need help? Post your questions and find answers here.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Math error using Macro inline without outer ()'s

Post by skywalk »

I had to increase the code complexity and "no debugger" to see a difference on my PC.
Speed tests are complex in a multitasking operating system.
Compiler optimizations, order of tests, antivirus sniffing affect outcomes.
These results are similar to what v6 revealed with the C backend + optimizer. ASM is 2nd place.
Can't wait to see the optimizer speeds when volatile is fixed!
---------------------------
Macro vs Procedure :: CBE+opt :: 1e9 runs M-P-M-P
Macro - 1850,1964, Proc - 2429,2209
Macro - 1857,1984, Proc - 2384,2224
---------------------------
Macro vs Procedure :: ASM :: 1e9 runs M-P-M-P
Macro - 2451,2412, Proc - 4621,4545
Macro - 2419,2402, Proc - 4552,4539
---------------------------
Macro vs Procedure :: CBE :: 1e9 runs M-P-M-P
Macro - 2582,2432, Proc - 5521,5539
Macro - 2811,2453, Proc - 5599,5506
---------------------------

Code: Select all

Macro IsEqual(x, y, small1=1e-9)
  Bool(Abs((x)-(y)) <= small1)
EndMacro
Procedure.d ProcedureTest(x.d,y.d)
  ProcedureReturn Bool(Abs((x)-(y)) <= small1)
EndProcedure
ntries.q = 1e9
n.d=1
startm1.q=ElapsedMilliseconds()
For c=1 To ntries
  n=IsEqual(c/2,c-100)
Next
stopm1.q=ElapsedMilliseconds()-startm1
n.d=1
startp1.q=ElapsedMilliseconds()
For c=1 To ntries
  n=ProcedureTest(c/2,c-100)
Next
stopp1.q=ElapsedMilliseconds()-startp1
n.d=1
startm2.q=ElapsedMilliseconds()
For c=1 To ntries
  n=IsEqual(c/2,c-100)
Next
stopm2.q=ElapsedMilliseconds()-startm2
n.d=1
startp2.q=ElapsedMilliseconds()
For c=1 To ntries
  n=ProcedureTest(c/2,c-100)
Next
stopp2.q=ElapsedMilliseconds()-startp2
MessageRequester("Macro vs Procedure", "Macro - " + StrD(stopm1) + "," + StrD(stopm2) + ", Proc - " + StrD(stopp1) + "," + StrD(stopp2))
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Math error using Macro inline without outer ()'s

Post by BarryG »

Found the post where Fred said that Procedures do have some speed overhead:
Fred wrote: Sat Aug 09, 2003 12:59 pmwhen you put code in a procedure, there is code 'around' the procedure (especially registers saving) which can take some times
Link -> viewtopic.php?p=31871#p31871
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Math error using Macro inline without outer ()'s

Post by STARGÅTE »

BarryG wrote: Sat Nov 26, 2022 5:29 am Found the post where Fred said that Procedures do have some speed overhead:
Of cause there is an overhead and there are situations, where you can replace a procedure with a macro.
I do it myself, too. However, this should not be the normal case.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Math error using Macro inline without outer ()'s

Post by skywalk »

For frequently called code, the Macro() is too fast to avoid. In fact, optimizers attempt to duplicate this performance by inlining Procedures when possible. :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply