A/B testing functions

BlitzMax Forums/BlitzMax Programming/A/B testing functions

Grey Alien(Posted 2008) [#1]
Just did some good optimisation by having two copies of a function which I could test 100,000 times via two different key presses. Then I optimised one of them and got the time down.

However, one of the optimisations I tried didn't work, it was slower in fact. In this thread: http://www.blitzbasic.com/Community/posts.php?topic=76454 I talked about having a method with 10 params that I call in a loop from another method and if it would be quicker to have no params and use Global variables (well actually fields on the type that the method is part of) instead. Well I tried it out and the fields method was slightly slower! I conclude this is because fields/globals are basically in "slow memory" i.e not the CPU registers or stack or cache or something and so it's actually faster to pass tons of local variables into a method instead of using globals. Worth knowing that this is a false optimisation as I won't bother in the future.


Blueapples(Posted 2008) [#2]
Well to look up a field you have to follow a pointer or two to get to the actual memory. Parameters are on the stack. So fields on an object vs true globals are not the same thing at all.

You could try embedded functions, and use function "globals" (actually, statics) to pass the values to the function. Don't know this technique would apply to your situation.

I'm not sure where static variables are actually stored. Probably on the heap since the stack would be unwound and they preserve their values between calls. I would expect it might be faster than pushing / popping params onto the stack on every call though.

Method A()
   Global ParamA:int
   ParamA = 100
   Funcion F()
     Print ParamA
   EndFunction

   For i:int = 1 to 100
     F()
   Next
EndMethod



I guess if you're really this worried about it you might want to write this bit as a C module.


Grey Alien(Posted 2008) [#3]
I wasn't actually testing fields on an object vs true globals. I was testing locals in a method passed into another method vs fields on an object (and I also tried globals on an object). passing locals in was fastest.

I tried embedded functions before but it crashes big time. It's not supported apparently, wish it was as that's what I tried first. I posted about it around a year and a bit ago.


Blueapples(Posted 2008) [#4]

I wasn't actually testing fields on an object vs true globals. I was testing locals in a method passed into another method vs fields on an object (and I also tried globals on an object). passing locals in was fastest.



I understand that but it sounded like the suggestion was to use globals to increase speed. My point was that might actually be faster, and there is no reason to believe that fields on an object would be any faster than passing parameters to a function.

I tried embedded functions before but it crashes big time. It's not supported apparently, wish it was as that's what I tried first. I posted about it around a year and a bit ago.


Embedded functions are not only supported, they are used in BRL's own module code (for instance, see CopyDir in BRL.FileSystem). I've also used them in a parser to hide each predicate handling function from the global namespace. There are some things you can't do with embedded functions, but they are supported.


Grey Alien(Posted 2008) [#5]
My point was that might actually be faster, and there is no reason to believe that fields on an object would be any faster than passing parameters to a function.
Ah OK, I didn't try with PURE globals as opposed to globals in a type. The thing is the method I was testing was part of the same type so I assumed it would be easy for the compiler to reference the type's globals.

There are some things you can't do with embedded functions, but they are supported
I'm 100% sure it didn't work before. I ought to find my bug report/post via a search. I was probably doing something that isn't supported. I think you are not allowed to have a sub-function in a method of a type something like that.

OK found it:

http://www.blitzbasic.com/Community/posts.php?topic=63020#704731