Fog Creek Software
Discussion Board




VB faster than C++??

There was a recent thread talking about speed of C and C++.

I find that VB can actually run some things faster than C++

I just tried the following loop in VB (version 6).

The loop is to 2 billion. When I turn off all the error checking stuff to optimize speed in VB, it runs very fast. The loop runs in about 7 seconds. (actually 6.8). I am using a pIII 600mhz.

The vb code was simple

dim i as long
for i = 1 to 2000000000
next i

In C++ (version6), I can’t get the loop to run in less then 20 seconds. Anyone have a suggestion as to how to get the following loop to run faster? If I turn on all the optimization options, then the loop of course does not run, since the compiler sees that nothing needs to be done! If I change the long t2 to Volatile long, then the loop does run, but again it is *much* slower than VB. Any hints how to get C++ to run this faster than VB?

My only point here, is that one would expect a simple loop in C++ to be *much* faster. Of couse a simple little loop does not tell the whole story either...but I am new to C++, and was very surprised to see that VB6 was able to run this loop faster than VC6

int main(int argc, char* argv[])
{
long t2;
char j[50];

cout << "Type go, then hit enter to start " << "\n";
cin >> j;

for (t2 = 1;t2 < 2000000000; t2++);

cout << “done” << "\n";

return 0;
}


Albert D. Kallal
Edmonton, Alberta Canada
Kallal@msn.com

Albert D. Kallal
Tuesday, May 07, 2002

Since VB and C++ use the same back end, I would (normally) expect similar performance. Why don't you disassemble the output and see what's different?

Joel Spolsky
Tuesday, May 07, 2002

"If I turn on all the optimization options, then the loop of course does not run, since the compiler sees that nothing needs to be done!"

Bingo - so C++ is infinitely faster than VB. :]

Dan Maas
Tuesday, May 07, 2002


Agree... with optimizations turned on (for release you should always optimize) C++ is several times faster than VB.

Also, I tried it on a very old server, with gcc 2.9. Ran in 15 seconds. Then moved some variables, declared "register long" instead of long and it tooks about 10 seconds. Added some command line optimizations, and it lowered to 5 seconds.

I'm sure there is a good reason why VB is faster than C++ in your scenario. I have no idea what can be it, thought.

Leonardo Herrera
Tuesday, May 07, 2002

"If I turn on all the optimization options, then the loop of course does not run, since the compiler sees that nothing needs to be done!"

Are you certain that VB isn't doing a similar optimization?

Ged Byrne
Tuesday, May 07, 2002

Borland C++ 5.5.1 does it in just under 10 seconds (PIII 600, 256 MB RAM, Win98SE)

Have to try this in Delphi when I get home, as Delphi and C++ Builder us the same backend as well.

Brad Clarke
Tuesday, May 07, 2002

I added an extra variable and add to it in the loop to be able to turn the optimization back on.

#include <iostream>

int
main( void )
{
  long t2;
  char j[50];
  int dummy = 0;

  std::cout << "Type go, then hit enter to start " << std::endl;
  std::cin >> j;

  for (t2 = 1;t2 < 2000000000; t2++)
    dummy += t2;

  std::cout << "done adding up to " << dummy << std::endl;

  return 0;
}

This forces the optimizer to leave the loop in and actually step through it. Despite the extra work it is pretty fast, I did not measure it exactly, but I would guess it took about 6 secs (compared to 22 secs in the old version without optimization) (PIII, 550 MHz)

I do not have VB here and am not familiar with it either, maybe you want to check out the same operation there and compare again. As someone already mentioned (I guess), comparing "normally" compiled VB (with whatever optimization this includes) to unoptimized C++ is not quite fair :-)

Have fun,

Jutta Jordans
Tuesday, May 07, 2002

Here 's the Delphi code and the result

var
  i: Integer;
  StartTime: DWORD;
begin
  StartTime := TimeGetTime;
  for i := 0 to 2000000000 do
  begin
  end;
  ShowMessage(IntToStr((TimeGetTime - StartTime) div 1000));
end;

I got 18 sec on PII 350 with 128MB RAM Windows XP Prof.

Sunish
Tuesday, May 07, 2002

OOPS, Sorry, with Delphi its 13 sec and not 18 as I have told in the above post. Its too bad you can't modify your posting.

And its with the default IDE settings which implies optimization is turned on.

Sunish
Tuesday, May 07, 2002

A VB6 Long is 32-bit, while a VC++6 long is 64-bit. This may have some impact on their relative performance on 32-bit CPUs.

If you know VB4-6 well, and care about performance, you use Long where a C++ programmer would use int.

Dave Rothgery
Tuesday, May 07, 2002

How did Delphi get involved in this discussion?  While you're at it, why don't you give us some COBOL or Powerbuilder time trials  ?

Bella
Tuesday, May 07, 2002

Umm, no - a VC 6.0 long is 32 bits, not 64.

Chris Tavares
Tuesday, May 07, 2002

Somebody using both Delphi and C++ Builder was curious to know.

BTW The time taken can vary considerably with no application running. I did the test within a loop 100 times and got an average of 11555.93069 milliseconds.

With other apps running it varied from 11 to 24 seconds.
So perhaps the results do vary with other factors

Sunish
Tuesday, May 07, 2002

In vc6 a long is 4 bytes by default.

Also, using clock() will yield much more accurate results.

Johnny Simmson
Tuesday, May 07, 2002

You cannot say that the VB compiler generated better code when you give it the opportunity to optimized but denied the C++ compiler to do the same. And as you stated yourself. the C++ compiler did much better when it was allowed to. Comparing unoptimized code is of little practical interest.

Using an empty loop as the benchmark subject only tests the optimization known as "dead code elimination". It also is very prone to clocking error.
However, dead code is not very common (except perhaps in templates).

You could rather perform test on loops that does something "meaningful", like e.g. the fibonacci sequence:
---
Dim eldest As Double, elder As Double
Dim youngest As Double
Dim i As Long, j As Long

For j = 0 To 19999
  elder = 1#
  youngest = 1#

  For i = 0 To 999
    eldest = elder
    elder = youngest
    youngest = elder + eldest
  Next i
Next j

MsgBox CStr(youngest)
---
This code should perform about the same in VB and (when ported of course) in C++, because the backend can hold all variables in registers. Then consider the same code written a little more convoluted using arrays:
----
Dim a(0 To 2) As Double
Dim i As Long, j As Long
For j = 0 To 19999
  a(0) = 1#
  a(1) = 1#
  For i = 0 To 999
    a((i + 2) Mod 3) = a((i + 1) Mod 3) + a(i Mod 3)
  Next i
Next j
MsgBox CStr(a((999 + 2) Mod 3))
----
Try this code in VB and weep...

Roland Kaufmann
Tuesday, May 07, 2002

Heres the result for Visual FoxPro 7 :

4minutes 2seconds on a Duron 850Mhz / 256MB / WinXP.

Chris O
Tuesday, May 07, 2002

"How did Delphi get involved in this discussion? "

I brought it up.

I don't use Microsoft development tools anymore (dropped them in 1996), but I do use a set of tools that share a common back end same as VB and VC++ do.

I compiled the C++ code at work with Borland C++ 5.5.1 and was curious to see what the result would be with Delphi, using the same backend as BC++.

Brad Clarke
Tuesday, May 07, 2002

"Here 's the Delphi code and the result

I got 18 sec on PII 350 with 128MB RAM Windows XP Prof. "

I wrote that code as a console app, and got 4 seconds on an AMD K6-2+ 450 (overclocked to 500) with 448 MB RAM, W2K Pro, Delphi 5 Pro.

Brad Clarke
Tuesday, May 07, 2002

>>Roland Kaufmann wrote...
>>Try this code in VB and weep...

Again, not much difference. In fact, in VB it does it in 3.7 secs (again, optimized, and error checking turned off in VB). This again is quicker than C++ (it was about 5 seconds). On the other hand, I don't really know the compiler options in C++ yet (I was not able to run on some of the speed options for C++, as it started complaining). I am not at all being fair to the C++ as it is not my home environment.

The main point again here is that simple loop stuff in VB is not that far off of C++ (and in my examples VB6  was faster than VC6). Remember, older versions of VB did not have a true *native* compiler and used p-code. VB6 DOES have a native compiler, and thus general looping stuff really should not run much slower than C++ anyway. When you compile in VB, you have a choice of p-code or native. When you choose native, it does seem to give c++ a run for the money.

At the end of the day, most software spends 80% of its time in windows code anyway. I just came in to c++ thinking that is would be *much* faster than VB. Perhaps there is a stigma against VB, and that much of this is from the way things worked in the past.


Albert D. Kallal
Edmonton, Alberta Canada
Kallal@msn.com

Albert D. Kallal
Tuesday, May 07, 2002

This has got to be one of the stupidest conversations I have ever read. Are any of you clowns typing this in and testing it actually programmers?

Who the hell cares how fast an empty loop loops? It's meaningless. The point of speed in these languages is that in VB, you cannot make certain kinds of optimizations that you can make in C.  You can't inline assembly if you want to, you can't hint register usage, you can't elect to shuffle pointers rather than data, you can’t forcibly avoid external calls by implementing runtime functions in your own source, you can't (easily) control threading, you can’t control stack/heap use, etc. You get whatever “Make EXE” gives you, which may be pretty good, but it’s not negotiable.

You'd be hard pressed to find a speed difference in the average application implemented in VB or VC++. The difference is that when performance is a must, you are *able* to optimize C++ with far greater control than with

Troy King
Wednesday, May 08, 2002

I could not agree more with Troys comments. The issue of performance in c++ is not one of simple instruction speed, but one of a tool that allows one to more closely interact
with the computer.

However, what is interesting that hopefully more people will walk away realizing this very issue. There is very much a myth about C++, and how fast it executes. It is not the execution speed of a general instruction that gives C++ its advantage.

This all of course will disappear with vb.net, and C#, as they will actually have the same functionality and speed. (and share the common runtime).

Albert D. kallal
Edmonton, Alberta Canada
kallal@msn.com

Albert D. Kallal
Wednesday, May 08, 2002

>The difference is that when performance is a must, you are *able* to optimize C++ with far greater control than with

yup, agreed, but to optimize correctly you should be aware of what takes how much of your time, and while I would go about it with a profiler normally, I really like to type things like those given here in to get a feeling of how fast they get with and without optimization and look at what the optimizer produces from those examples.

At our company we do a lot of image processing and much of the older code was written in assembly (for optimization reasons of course). Normally I am able to speed it up by just translating it back to C++ and let the compiler do the optimizing for me, but not always. To learn what your optimizer can handle efficiently and what it can't is IMHO rather interesting.

BTW, I do not trust anything someone states about this being much faster than that without trying it out myself normally. I find it funny and rather educating.

Have fun,

Jutta Jordans
Wednesday, May 08, 2002

You're a real thread-killer, Troll King.

dunno wair
Thursday, May 09, 2002

Hey Albert could you post the source for the bechmarking?

Thanks

Pierre
Sunday, May 12, 2002

Gee, the source for the bench marking! That sounds so official. These are just simple looping things. However, the point made here is that we are simply executing a few instructions in VB, or C++. This does not tell the story, but it certainly demonstrates that C++ is not faster than VB due to executing some simple instructions. It (*can* be faster because it is a different tool, and allows one to interact with the computer in a different way. People should not be shocked that VB run this fast....as it not why C++ has the upper hand.

I also mentioned, I am new to C++, and thus don’t know to really speed it up.

Anyway, the loop code was posted. The 2nd VB stuff (that was supposed to be slow, and again ran faster via VB then the C++) is re-posted also.

int main(int argc, char* argv[])
{
    double a[3];
    long i;
    long j;
    char myin[50];

  cout << "type go, then enter to continue";
  cin >> myin;

    for (j=1;j < 20000;j++)
    {
        a[0] = 1;
        a[1] = 1;

        for (i=0;i < 1000;i++)
            a[(i + 2) % 3] = a[(i + 1) % 3] + a[i % 3];
    }
        
    cout << "done" <<"\n";
    
    return 0;
}

Here is the VB

Dim ti      As Single
   
    Dim a(0 To 2) As Double
    Dim i As Long, j As Long
   
    ti = Timer
For j = 0 To 19999
        a(0) = 1
        a(1) = 1
        For i = 0 To 999
            a((i + 2) Mod 3) = a((i + 1) Mod 3) + a(i Mod 3)
        Next i
    Next j
   
    MsgBox Timer - ti
    MsgBox CStr(a((999 + 2) Mod 3))

Albert D. Kallal
Monday, May 13, 2002

Albert, you still missed some of the point. Given that VB and C++ are both compiled languages, if you're able to compare anything, at best it will be how well each compiler generates code.

As it was pointed out earlier, if you actually build a release-optimized image from your sample code, every decent C++ compiler will remove the useless loop anyway.

If you're coming over from the world of VB, and you're looking for night-and-day speed differences, try PowerBasic ( http://www.powerbasic.com/ ). It has BASIC syntax, provides all the datatypes used by the Win32 API, builds the smallest, fastest Windows apps this side of hand-optimized Assembler, and doesn't require an external runtime.  It also has built-in keywords for regular expressions, TCP/IP, and other nifties.

Troy King
Tuesday, May 14, 2002

LMAO

This must be a joke.  Just looking at the source to the C++ "equivalent" made me laugh.  main()???

Are you writing a comparable multi-threaded Win32 application, or a (likely debug release) Virtual-8086 mode single-threaded console application?  Talk about stacking the deck.

Always Always ALWAYS take the OS and its behavior into consideration when benchmarking.

And NEVER believe something that defys common sense.  Someone saying VB is faster than C or C++ at doing anything is smoking crack.  Side by side in an honest comparison, C or C++ will always win, unless VB somehow manages to beat C or C++ to implementing faster instructions on a new backward-compatible Intel archictecture.

The more a language implementation hides from its user, the less chance it has for specific-case optimization and thus, the slower it is.

The part I find most amusing is that the claim is made in the shadow of a C/C++ based OS.  But what I find most unbelievable (and at the same time, shameful) is that some so-called C/C++ programmers are actually swallowing this bs whole.

James Jackson
Monday, May 19, 2003

Oh pfft,

VB is a load of Korean illiterate crap that deserve's no respect and should be banned.

It is for learning to program.

Use a REAL language, like C++.

VB is just a joke

GG, Owned~
Tuesday, January 13, 2004

From Version 6 and up, VB IS a good tool for rapid W32 application development. You would normally need MUCH more time to write and debug an equivelant C/C++ application. So, if one needs to develop a W32 app FAST and (doesn't need cross-platform compatibility and/or doesn't plan to seriously tamper with hardware settings and/or doesn't need top-notch performance), then VB is definitely a decent option.

greetings.

S. Sirigos
Wednesday, March 03, 2004

lol, what is it with people's hate for VB?

From what I gathered, the way the computing world has gotten to is that if code runs slower than it should, then they tell the person to upgrade their CPU/RAM/Graphics Card...

The time it takes for optimisations and/or learning lower level languages then what you're proficient at is far more costly than telling someone that they need to upgrade their PC to XXXX Mhz with YYYY Mb RAM...

I know all about good programming practices etc, but let's just look at Microsoft Products as an example here...MSN 6.1 grinds my PC down to a halt!
It's an AMD AthlonXP 2400, 1280Mb RAM, 64Mb DDR ATi Radeon graphics card, and this thing EATS it! It's an instant messaging program for God's sake!

Stick with what you know. a few seconds here and there arent the end of the world, cos in 18 months the hardware required to run your program at twice the speed will be half the price as the current crop of PC's...

Windows 3.11 on an AthlonXP is obscene...

Peter Lewis
Thursday, March 11, 2004

oi dopey balls.. optimization..

try ripping a dvd using code written in say........

visual basic.


how long u think it would take?

things need to be optimized one way or another.. who wants a slow pc? as for msn messenger grinding ur a box to a halt.. get a decent motherboard perhaps ?

akkit wang
Monday, April 12, 2004

Simply put: 
If I need to make a powerfull program that will be able to crunch a lot of data in a short amount of time, I choose c++.
However, if I need something real quick and on the go, that looks pretty and I can create in a snap, I choose VB.
Enjoy,

Scott Weisel
Friday, April 16, 2004

finally some people who share my thoughts. size does matter! bloatware sux!

greetz,
chaozz
www.under1mb.com
freeware under 1mb in size

chaozz
Thursday, May 06, 2004

I was browsing this forum, looking for an old trick on how to include ASM code in a Visual Basic project by manipulating the shell calls to VB's linker, and tossing in an .obj created in a lang that supports inline assembly.  (I haven't found it yet, I suppose I'll have to dig into my old Magazine archives... it was in MSJ or VBPJ at some point, as I recall.)

Anyhow, I wanted to stop reading this conversation, but it was like a train wreck... too horrible to watch, too intriguing to look away... lol.  Programmers new and seasoned bickering over which is better, VB or C++... for shame... lol.

The bottom line folks, is this.  I've been writing code for over 20 years.  I've used just about every language at one point or another, and what I can tell any developer with absolutely confidence is that one lanugage IS NOT INHERENTLY BETTER THAN THE OTHER... (any bugs notwithstanding)... each language is useful for a specific purpose.  VB was created to be a RAD tool... and it's a *great* RAD tool.  When you have a custom business app to create for a client, nothing gets the job done faster than VB.  When you need to tweak the speed, you work in C, C++, ASM, depending on your needs and abilities.  In the event that you want to take advantage of the time savings that Rapid Application Development offers, while getting the very best of speed, the solution is simple: Create your "tight" routines in a tweakable language, like C/C++, put it in DLL format, and call your new fast functions from VB.  Or, if you have the inclination, use a more "genuine" approach to mixed-language development and use the linker trick I'm trying to find.  Time saved, job done well, money in the bank.

If you don't like that advice, well... that's your call, I guess, but I think any programmer worth their salt in the real world simply uses the right tool for the specific job, period.

rcryniak
Monday, June 21, 2004

BTW... I just found a link to the info... how to include ASM code in your VB executable by manipulating the Linker... it can be found at http://www.vbaccelerator.com/codelib/article/linkspf.htm

Enjoy!

rcryniak
Monday, June 21, 2004

*  Recent Topics

*  Fog Creek Home