Tag: optimization

(PB) Nearest Power of Two in Assembly

Posted by on August 4, 2011

On today’s masochism section… we find the nearest power of two in Assembly!

Macro PowerOfTwo( _num_ )
	!MOV     Eax, [v_#_num_]
	!SUB     Eax, 1
	!PowerOfTwo_Loop_#_num_:
	!MOV     Ecx, Eax
	!ADD     Eax, 1
	!AND     Ecx, Eax
	!JNZ     PowerOfTwo_Loop_#_num_ 
	!MOV [v_#_num_], Eax
EndMacro

Truth be told I was looking for some old libraries I wrote a while back and I stumbled upon that code. It made me chuckle so of course I had to share it. The reason I laughed was because, well… Who would really need to optimize such a routine?; in those days I was suffering from a severe case of premature-optimizationitis.

However, compared to the normal method which follows:

Procedure.i NearestPow2( Value.i )
	ProcedureReturn Pow( 2, Int((Log( Value ) / Log(2)) + 0.5) )
EndProcedure

The ASM routine is about 25 times faster (I benchmarked the code itself without the procedure to be fair, otherwise it would’ve been 55 times faster!)

So, while unnecessary it still proves a point — You can always optimize your code for speed. However, the cost is clear… readability!

I guess it’s worth stating the obvious in this case: Always benchmark your code and find a balance when it comes to optimization, only optimize your bottlenecks. Don’t waste time in small details unless the reward is worth the time.

Cheers!

PS: Since the PB pre-processor is case sensitive (who would’ve known!) Remember that you must pass the macro the same label your variable has, also because we define a label this means we can only call the routine in one place for one particular variable. That however was not an issue with my code, so it never bothered me.

Wikipedia entries, mostly biased.

Posted by on July 16, 2009

It was recently brought to my attention that the PB wikipedia entry is brutally biased towards marketing; it’s not maintained by the people but rather by themselves, the developers.

One of the key factors for them is that “their compiler makes small exes”… They never, ever, mention speed.

Some time ago I did a bunch of benchmarks, in a time where it was uncertain for me which language should I stick to (We all asked this question at least once) and I found some shocking results, but not surprisingly. You see, developing an efficient compiler is not an easy task. However, when someone talks their butt off a certain subject, be sure that they are hiding something else. More…

A fast vector library for purebasic.

Posted by on January 9, 2009

I recently required some vector functions to prototype a game idea in PureBasic, however there is no such thing as a built-in vector library and any of the existent community code was buggy to say the least (no offense but it’s old, ugly code that you’ll find anywhere else).

The reason for this library is quite simple, and so is the library itself. you can either include it or install the resource file and start using it right away. After taking a quick look at the source, you’ll soon familiarize with it’s naming convention; there is no need to write any documentation for this type of code, since it’s basic vector math. However a test source is included for your delight.

You can find the source in here: http://gushh.net/dev/?file=pb/vec3_macro.pbi
And some ugly test code in here: http://gushh.net/dev/?file=pb/vec3_macro_test.pb

Or, you can get “the whole package” from here: http://gushh.net/dls/vec3_macro.zip

Using this library is as easy as…

Define.VEC3 a, b, c
VEC3_SET( a, 0.1, 0.2, 0.3 )
VEC3_SET( b, 0.4, 0.5, 0.6 )
VEC3_ADD( a, b, c )
VEC3_DEBUG3( a, b, c ) ; display the vectors in the debug console.

 
More…

URL Encoding in PureBasic, the proper way.

Posted by on March 30, 2008

The other day I found myself in the need of a compliant url-encoding routine for the PB language, not surprisingly theres none available by default (there are actually no specialized libraries whatsoever, and the little you see is implemented from third-party code…).

So I had to code my own, as always. When no proper framework is given, you have to either depend on others to implement it or just do it yourself, since the former requires time and patience… I always choose the latter 🙂

Heres a standards compliant routine to URL-Encode URIs in PureBasic:

The url_encode() code can be found at > here <

More…

Faster ways of finding a character inside a string, in PureBasic.

Posted by on March 3, 2008

For those of you who either prototype or work with PureBasic on a daily basis, if you ever found yourself looking for faster ways of performing string manipulation, while still using the string system this language provides, be glad for I’ll be posting a few of my solutions to speed up the process.

My first entry is the FindChar() routine. Unlike the official FindString(), this one only searches for a single character. In situations where you’ll be dealing with single character string searches rather than multi-character, this routine will give you up to 2x speed increase in both Ascii and Unicode modes. Worst case scenario, you’ll get equal results speed-wise.

The FindChar() code can be found at > here <

More…