Page 1 of 2
Compare performances Blitz - PureBasic and C++
Posted: Thu Oct 21, 2004 1:33 am
by Nikko
Does anyone could give me an idea why C++ compiled is so much faster than basics. The purebasic can generate an .ASM, and the code seems to be pretty optimized! Is there an error in my code???
Blitz
execution : 8004 ms
size exe: 1 290 240 octets
PureBasic
execution : 7656 ms
size exe: 6144 octets
C/C++
execution : 31 ms
size exe : 45 056 octets
Here are the source codes
Code: Select all
;blitz basic
StartTime=MilliSecs()
y#=123456789
j=1
t=0
u=0
For i=1 To 10000000
If j=1 Then j=2
If j=2 Then j=4 Else j=1
t=t+256
u=u-256
y#=y#+y#/3
Next
Print MilliSecs()-StartTime
Delay(2000)
Code: Select all
; PureBasic
OpenConsole()
StartTime=ElapsedMilliseconds()
y.f=123456789
j=1
t=0
u=0
For i=1 To 10000000
If j=1
j=2
EndIf
If j=2
j=4
Else
j=1
EndIf
t=t+256
u=u-256
y.f=y.f+y.f/3
Next i
ElapsedTime = ElapsedMilliseconds()-StartTime
Print (Str(ElapsedTime))
Delay(2000)
Code: Select all
#include "stdafx.h"
#include "math.h"
#include "time.h"
#include "stdio.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i=0,j=1,x=0,u=0,t=0;
int StartTime=clock();
double y=123456789;
for (i=1;i<10000000;i++)
{
if (j==1) j=2;
if (j==2) j=4; else j=1;
t=t+256;
u=u-256;
y=y+y/3;
}
x=clock()-StartTime;
printf("time=%d", x);
getchar();
return 0;
}
Posted: Thu Oct 21, 2004 3:34 am
by fsw
When I compile your PB code to exe the result is 480ms on a 950Mhz Duron Laptop.
Re: Compare performances Blitz - PureBasic and C++
Posted: Thu Oct 21, 2004 5:51 am
by IceSoft
Try this one again:
PB code improved
C code corrected
Code: Select all
; PureBasic
OpenConsole()
StartTime=ElapsedMilliseconds()
y.f=123456789
j=1
t=0
u=0
For i=1 To 10000000
If j=1
j=2
EndIf
If j=2
j=4
Else
j=1
EndIf
t+256
u-256
y.f+(y.f/3)
Next i
ElapsedTime = ElapsedMilliseconds()-StartTime
Print (Str(ElapsedTime))
Delay(2000)
Code: Select all
#include "stdafx.h"
#include "math.h"
#include "time.h"
#include "stdio.h"
int _tmain(int argc, _TCHAR* argv[])
{
int StartTime=clock();
int i=0,j=1,x=0,u=0,t=0;
double y=123456789;
for (i=1;i<=10000000;i++)
{
if (j==1) j=2;
if (j==2) j=4; else j=1;
t=t+256;
u=u-256;
y=y+y/3;
}
x=clock()-StartTime;
printf("time=%d", x);
getchar();
return 0;
}
Posted: Thu Oct 21, 2004 8:38 am
by Rings
remember a enabled debugger slow down your code !
Re: Compare performances Blitz - PureBasic and C++
Posted: Thu Oct 21, 2004 9:06 am
by Max.²
IceSoft wrote:Try this one again:
PB code improved
C code corrected
The PB version runs 160ms on my machine. What I could imagine is that the C compiler royally optimizes the code, like compiling the whole loop to static data, which would explain the size of the C exe.
What C compiler are you using and what is "stdafx.h" ?
Edit: Compiling it with the Mingw C Compiler, the exe runs 190ms and consumes 20kb.
Using PellesC, it is 170ms and it has a size of 26KB.
Ah, and I think I found the compiler you used. The Microsoft Compiler from the free Visual C Toolkit creates an 45kb exe which runs 40ms with the options of /O2 /Ox.
Posted: Thu Oct 21, 2004 9:08 am
by GedB
Nikko,
Which C++ compiler are you using.
Some of the very clever C++ compilers optimise out dead code.
For example, you assign to local variables j, t, u and y but you never read them.
A very good optimiser will realise that if nothing reads these variables, and they are lost once they go out of scope, it is a waste of time assigning to them.
If you apply this logic, youre loop doesn't actually do anything at all, and may be removed.
Posted: Thu Oct 21, 2004 9:50 am
by Fred
GedB explaination is probably the right one. MS VC++ compiler can detect useless loop and remove them from the final executable. It's a good optimisation for benchmarking, but almost useless in a real case code.
Posted: Thu Oct 21, 2004 10:48 am
by GedB
If this is the optimisation taking place, the C++ code is optimised to this:
Code: Select all
#include "stdafx.h"
#include "math.h"
#include "time.h"
#include "stdio.h"
int _tmain(int argc, _TCHAR* argv[])
{
int StartTime=clock();
x=clock()-StartTime;
printf("time=%d", x);
getchar();
return 0;
}
What on earth is in executable to make it so big?
Posted: Thu Oct 21, 2004 1:47 pm
by Dare2
Fred wrote:GedB explaination is probably the right one. MS VC++ compiler can detect useless loop and remove them from the final executable. It's a good optimisation for benchmarking, but almost useless in a real case code.
Even counter-productive. If I write (even for a crappy reason):
Code: Select all
Procedure useLessLoop()
For i=1 to 10000
a+1
Next
EndProcedure
Then that is what I want., not an empty procedure.

Posted: Thu Oct 21, 2004 10:25 pm
by aaron
Dare2: in that case, you should always compile with optimization turned completely off. The idea behind optimization is that the compiler knows better than the user.

Posted: Thu Oct 21, 2004 11:34 pm
by Dare2
aaron wrote:Dare2: in that case, you should always compile with optimization turned completely off. The idea behind optimization is that the compiler knows better than the user.

True. (Especially the bit about knowing better .. than this user, at least)
But until we get optimisation option in PB, let PB it do as it is told.
(Or do we have that option hidden somewhere? - prepares blush icon)
Posted: Fri Oct 22, 2004 1:15 am
by fsw
fsw wrote:When I compile your PB code to exe the result is 480ms on a 950Mhz Duron Laptop.
If you change in Nikko's code this:
to that:
the whole thing gets slower, it changes to 781ms.
Why? I thought longs are faster processed than floats...
Posted: Fri Oct 22, 2004 12:40 pm
by aaron
Same here... mine goes from 221 (no debugger) to 380 with longs instead of floats. I guess the floats are handled faster in the FPU than purebasic deals with longs. Strange.
Athlon 2400+ (1800MHz actual), 512M memory, laptop.
Posted: Fri Oct 22, 2004 2:41 pm
by Karbon
Don't modern processors have specific instruction sets and such for floating point math and such now? I thought I read somewhere that moderm CPUs have reversed the old school thinking that floats are slower by optimizing the hardware around them..
Posted: Fri Oct 22, 2004 4:21 pm
by Moonshine
Like 3DNow and such