Compare performances Blitz - PureBasic and C++

Everything else that doesn't fall into one of the other PB categories.
Nikko
New User
New User
Posts: 1
Joined: Thu Oct 21, 2004 1:29 am

Compare performances Blitz - PureBasic and C++

Post 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;
}
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

When I compile your PB code to exe the result is 480ms on a 950Mhz Duron Laptop.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Compare performances Blitz - PureBasic and C++

Post 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;
}
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

remember a enabled debugger slow down your code !
SPAMINATOR NR.1
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Re: Compare performances Blitz - PureBasic and C++

Post 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.
Last edited by Max.² on Thu Oct 21, 2004 9:15 am, edited 1 time in total.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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. :)
@}--`--,-- A rose by any other name ..
aaron
Enthusiast
Enthusiast
Posts: 267
Joined: Mon Apr 19, 2004 3:04 am
Location: Canada
Contact:

Post 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. :wink:
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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. :wink:
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)
@}--`--,-- A rose by any other name ..
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post 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:

Code: Select all

y.f=123456789 
to that:

Code: Select all

y.l=123456789 
the whole thing gets slower, it changes to 781ms.

Why? I thought longs are faster processed than floats...
aaron
Enthusiast
Enthusiast
Posts: 267
Joined: Mon Apr 19, 2004 3:04 am
Location: Canada
Contact:

Post 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.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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..
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Moonshine
Enthusiast
Enthusiast
Posts: 263
Joined: Tue May 25, 2004 12:13 am
Location: UK

Post by Moonshine »

Like 3DNow and such
Mark my words, when you least expect it, your uppance will come...
Post Reply