Re: An illuminating experiment using platform().
- Posted by Unkmar Jul 13, 2008
- 833 views
The topic of platform() getting depricated has come up again. If there is one thing that should be depricated it is the depricate function. It keeps getting called at random and is only useful for breaking software.
Seriously, there is little cost involved using platform() as compared to say ifdef with the correct parameters. I wrote a seven line program called example:
include os.e if platform() = WIN32 then printf(1, "Hello Windows", {} ) end if if platform() = DOS32 then printf(1, "Hello DOS32", {} ) end if if platform() = LINUX then printf(1, "Hello LINUX", {} ) end if
Okay, so we will expect to see a comparison in the C code jumping over some display code after we compile. A branch seems like a little thing but some say that it is too ineficient to let us keep it. Just for curiosity, lets see how the branch is implemented. Is it a goto? I translated it using the options -plat DOS -wat and got this:
// ifdef WIN32 then
// if platform() = WIN32 then
// if platform() = DOS32 then
// printf(1, "Hello DOS32", {} )
EPrintf(1, _1153, _10);
L1:
// if platform() = LINUX then
Cleanup(0);
So, as you can see, the EPrintf() is there to replace the EUPHORIA printf but only the DOS32 version is there. The branches that include platform() = to other platforms are not even included.
There is absolutely no branching cost of using platform() rather than ifdef when translating to C.
Shawn Pringle B.Sc.
Let's take a look at a slightly more realistic issue.
include os.e for idx = 1 to 123456 if platform() = WIN32 then printf(1, "Hello Windows", {} ) end if if platform() = DOS32 then printf(1, "Hello DOS32", {} ) end if if platform() = LINUX then printf(1, "Hello LINUX", {} ) end if end for --Versus ifdef for idx = 1 to 123456 ifdef WIN32 then printf(1, "Hello Windows", {} ) end ifdef if DOS32 then printf(1, "Hello DOS32", {} ) end ifdef ifdef LINUX then printf(1, "Hello LINUX", {} ) end ifdef end for
The platform() example remains as 3 if tests for all 123456 iterations of the idx loop. The ifdef gets reduced to a single print statement within the loop. I believe that we can all clearly see that it will execute many times faster. It simply has less to do. Some of you will say that this isn't a realistic example. Imagine that, instead of it being a for loop. It is a procedure or function that contains the ifdef or platform() test. Maybe sockets for Windows versus Linux. With platform(), every time the function is called, the decision must be made. However, using ifdef, the decision is made once and only the applicable code is applied.
Unkmar - Lucius L. Hilley III
PS: Creole did not wish to allow me to format my signature to my liking. Apparently {{{ must be on a line all by itself.