Home All Groups Group Topic Archive Search About

The inaugural VB6 vs dot net test

Author
29 Nov 2007 10:52 AM
Michael C
For everyone who hasn't been following the other exciting thread this was
the challenge. A comparison between the speed of VB6 and dot net. The
challenge is to invert every pixel of a bitmap pixel by pixel. The bitmap
should be 1024x768x24bit. Contents of the bitmap are not important although
I think if someone can prove that it makes a difference then we should all
use the same bitmap. The bitmap should be loaded into memory and inverted
1001 times and the total time it takes to do all the inverting recorded.
Whatever variables/structures etc that are used to invert the bitmap must be
destroyed and recreated for each of the 1001 iterations. I think it was
suggested that the code to invert the bitmap should be in a function and
just the bitmap (or pointer to) should be passed to it. Anything that
modifies pixels en masse such as an API call is excluded. The function must
take into account the padding at the end of each line of the bitmap (even
though it is zero for 1024 pixels) and must work with bitmaps of different
sizes without re-coding. Hard coding it to 24 bits is acceptable.

It appears that Mike William and Olaf are up for the challenge. I guess I'll
be in the other corner on my own :-). So here goes, the results of my test
are 21 seconds for 1001 iterations or 48fps. Not bad considering it's a
managed language and I was using all native code (no APIs).

For anyone who says this test is just a test of one small part of dot net
and it will be significantly slower in other tasks, yes, you are right so
don't bother. This is a small test of what dot net is capable of.

Michael

Author
29 Nov 2007 11:31 AM
Michael C
Show quote
"Michael C" <nospam@nospams.com> wrote in message
news:ONjacWnMIHA.4880@TK2MSFTNGP03.phx.gbl...
> For everyone who hasn't been following the other exciting thread this was
> the challenge. A comparison between the speed of VB6 and dot net. The
> challenge is to invert every pixel of a bitmap pixel by pixel. The bitmap
> should be 1024x768x24bit. Contents of the bitmap are not important
> although I think if someone can prove that it makes a difference then we
> should all use the same bitmap. The bitmap should be loaded into memory
> and inverted 1001 times and the total time it takes to do all the
> inverting recorded. Whatever variables/structures etc that are used to
> invert the bitmap must be destroyed and recreated for each of the 1001
> iterations. I think it was suggested that the code to invert the bitmap
> should be in a function and just the bitmap (or pointer to) should be
> passed to it. Anything that modifies pixels en masse such as an API call
> is excluded. The function must take into account the padding at the end of
> each line of the bitmap (even though it is zero for 1024 pixels) and must
> work with bitmaps of different sizes without re-coding. Hard coding it to
> 24 bits is acceptable.
>
> It appears that Mike William and Olaf are up for the challenge. I guess
> I'll be in the other corner on my own :-). So here goes, the results of my
> test are 21 seconds for 1001 iterations or 48fps. Not bad considering it's
> a managed language and I was using all native code (no APIs).
>
> For anyone who says this test is just a test of one small part of dot net
> and it will be significantly slower in other tasks, yes, you are right so
> don't bother. This is a small test of what dot net is capable of.

Looks like I might be a bit late although the previous comparisons are far
from perfect. Tom's C# code only inverts every third byte and his VB.Net
code copies 1 byte at a time through the marshaller when it should do it at
least one row at a time. Mike's code is using 32 bit ints where Tom's was
using 8 bits. Mine wasn't perfect either and the 21 seconds is actually more
like 6.5 after I removed a couple of bugs (150fps at 1024x768, not bad on a
Pentium 2.4). Mine was using 8bits also because I considered using a 32bit
int a bit of a cheat but might modify mine to see if it makes a difference.

Also, I have to object because mike hasn't provided jessica1024.bmp. It's
possible that different bitmaps give different results and I think it's only
fair we all use the same bitmap. :-)
Picture1.Picture = LoadPicture("c:\temp\jessica1024.bmp")

Here's my code anyway:
  private void button1_Click(object sender, System.EventArgs e)
  {
   Bitmap bitmap = new Bitmap(1024, 768, PixelFormat.Format24bppRgb);
   DateTime t1;
   t1 = DateTime.Now;
   for(int i = 0; i < 1001; i++)
   {
    InvertBitmap(bitmap);
   }
   DateTime t2;
   t2 = DateTime.Now;
   TimeSpan ts = t2 - t1;
   bitmap.Dispose();
   MessageBox.Show(ts.TotalMilliseconds.ToString());
  }

  private unsafe void InvertBitmap(Bitmap BitmapToInvert)
  {
   int w = BitmapToInvert.Width;
   int h = BitmapToInvert.Height;
   BitmapData data = BitmapToInvert.LockBits(new Rectangle(0, 0, w, h),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
   byte* ptr = (byte*)data.Scan0;
   int w3 = w * 3;
   int offset = w3 - data.Stride;
   for(int y = 0; y < h; y++, ptr += offset)
   {
    for(int x = 0; x < w3 ; x++, ptr++)
    {
     *ptr = (byte)~*ptr;
    }
   }
   BitmapToInvert.UnlockBits(data);
  }
Author
29 Nov 2007 11:53 AM
Michael C
"Michael C" <nospam@nospams.com> wrote in message
news:uTz8qsnMIHA.292@TK2MSFTNGP02.phx.gbl...
> "Michael C" <nospam@nospams.com> wrote in message

I did a bit more testing and got the following results:

VB.net 7.5 seconds (must say I'm suprised at this)
C# 6.6 seconds.
Mike's vb6 code 8.5 seconds.

The c# could be optimised to use 32bit int though so should be a bit faster.
Author
29 Nov 2007 1:15 PM
Michael C
Show quote
"Michael C" <nospam@nospams.com> wrote in message
news:uUthx4nMIHA.1208@TK2MSFTNGP05.phx.gbl...
> "Michael C" <nospam@nospams.com> wrote in message
> news:uTz8qsnMIHA.292@TK2MSFTNGP02.phx.gbl...
>> "Michael C" <nospam@nospams.com> wrote in message
>
> I did a bit more testing and got the following results:
>
> VB.net 7.5 seconds (must say I'm suprised at this)
> C# 6.6 seconds.
> Mike's vb6 code 8.5 seconds.
>
> The c# could be optimised to use 32bit int though so should be a bit
> faster.

I should add that mikes VB6 code could be optimised also. It should be
faster using a 1 dimensional array and counting down to zero.
Show quote
>
>
Author
29 Nov 2007 1:36 PM
David Kerber
In article <uUthx4nMIHA.1***@TK2MSFTNGP05.phx.gbl>, nospam@nospams.com
says...
> "Michael C" <nospam@nospams.com> wrote in message
> news:uTz8qsnMIHA.292@TK2MSFTNGP02.phx.gbl...
> > "Michael C" <nospam@nospams.com> wrote in message
>
> I did a bit more testing and got the following results:
>
> VB.net 7.5 seconds (must say I'm suprised at this)
> C# 6.6 seconds.
> Mike's vb6 code 8.5 seconds.

Pretty comparable, IOW.


--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
Author
29 Nov 2007 3:05 PM
Mike Williams
"David Kerber" <ns_dkerber@ns_WarrenRogersAssociates.com> wrote in message
news:MPG.21b88a51d6cfd441989bb7@news.conversent.net...

>> I did a bit more testing and got the following results:
>> VB.net 7.5 seconds (must say I'm suprised at this)
>> C# 6.6 seconds.
>> Mike's vb6 code 8.5 seconds.

> Pretty comparable, IOW.

Yep. It looks as though VB.Net is not as slow as I thought it might be, and
C Sharp is not as . . . erm . . . sharp as it was being made out to be, and
good old VB6 is up there giving them both a good run for their money and not
at all behaving like the "old codger" that certain people were making it out
to be ;-)

Mike
Author
29 Nov 2007 9:58 PM
Michael C
"Mike Williams" <mi***@whiskyandCoke.com> wrote in message
news:OXTF8kpMIHA.4912@TK2MSFTNGP06.phx.gbl...
> Yep. It looks as though VB.Net is not as slow as I thought it might be,
> and C Sharp is not as . . . erm . . . sharp as it was being made out to
> be, and good old VB6 is up there giving them both a good run for their
> money and not at all behaving like the "old codger" that certain people
> were making it out to be ;-)

Mike, can you modify your code to use a 1D array and have the for loops
count down to zero, or try using a while loop. Might be interesting to see
the results. I believe that a 2D array is slower because it's doing
unecessary multiplies for every pixel.
Show quote
>
> Mike
>
>
Author
29 Nov 2007 3:48 PM
Steve Gerrard
Show quote
"David Kerber" <ns_dkerber@ns_WarrenRogersAssociates.com> wrote in message
news:MPG.21b88a51d6cfd441989bb7@news.conversent.net...
> In article <uUthx4nMIHA.1***@TK2MSFTNGP05.phx.gbl>, nospam@nospams.com
> says...
>> "Michael C" <nospam@nospams.com> wrote in message
>> news:uTz8qsnMIHA.292@TK2MSFTNGP02.phx.gbl...
>> > "Michael C" <nospam@nospams.com> wrote in message
>>
>> I did a bit more testing and got the following results:
>>
>> VB.net 7.5 seconds (must say I'm suprised at this)
>> C# 6.6 seconds.
>> Mike's vb6 code 8.5 seconds.
>
> Pretty comparable, IOW.
>


The interesting thing to me is this part of the original post:

> "Michael C" <nospam@nospams.com> wrote in message
> news:ONjacWnMIHA.4880@TK2MSFTNGP03.phx.gbl...
>> For everyone who hasn't been following the other exciting thread this was
>> the challenge. A comparison between the speed of VB6 and dot net.
>>
>> For anyone who says this test is just a test of one small part of dot net
>> and it will be significantly slower in other tasks, yes, you are right so
>> don't bother. This is a small test of what dot net is capable of.
>

It is interesting because Michael's main routine,
  private unsafe void InvertBitmap(Bitmap BitmapToInvert)

is declared unsafe, since it reverts to using C pointers, and is *not* managed
code. The entire body of the loop, between locking and unlocking the bitmap, has
nothing whatsover to do with dot.net, it is just plain C.

Not that there's anything wrong with that. :)
Author
29 Nov 2007 8:51 PM
Tom Shelton
Show quote
On Nov 29, 4:31 am, "Michael C" <nos...@nospams.com> wrote:
> "Michael C" <nos...@nospams.com> wrote in message
>
> news:ONjacWnMIHA.4880@TK2MSFTNGP03.phx.gbl...
>
>
>
>
>
> > For everyone who hasn't been following the other exciting thread this was
> > the challenge. A comparison between the speed of VB6 and dot net. The
> > challenge is to invert every pixel of a bitmap pixel by pixel. The bitmap
> > should be 1024x768x24bit. Contents of the bitmap are not important
> > although I think if someone can prove that it makes a difference then we
> > should all use the same bitmap. The bitmap should be loaded into memory
> > and inverted 1001 times and the total time it takes to do all the
> > inverting recorded. Whatever variables/structures etc that are used to
> > invert the bitmap must be destroyed and recreated for each of the 1001
> > iterations. I think it was suggested that the code to invert the bitmap
> > should be in a function and just the bitmap (or pointer to) should be
> > passed to it. Anything that modifies pixels en masse such as an API call
> > is excluded. The function must take into account the padding at the end of
> > each line of the bitmap (even though it is zero for 1024 pixels) and must
> > work with bitmaps of different sizes without re-coding. Hard coding it to
> > 24 bits is acceptable.
>
> > It appears that Mike William and Olaf are up for the challenge. I guess
> > I'll be in the other corner on my own :-). So here goes, the results of my
> > test are 21 seconds for 1001 iterations or 48fps. Not bad considering it's
> > a managed language and I was using all native code (no APIs).
>
> > For anyone who says this test is just a test of one small part of dot net
> > and it will be significantly slower in other tasks, yes, you are right so
> > don't bother. This is a small test of what dot net is capable of.
>
> Looks like I might be a bit late although the previous comparisons are far
> from perfect. Tom's C# code only inverts every third byte

Yes, in my haste, I screwed that up.  In fact, it does the same in the
VB.net code.  But, I will point out that my iteration was correct, and
your code is slower then mine - even though I gain about a second when
I do it correctly.  Here is the corrected C# :

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main ( string[] args )
        {
            Bitmap bmp = new Bitmap ( "blizz2001-1024.bmp" );
            Stopwatch sw = new Stopwatch ();

            sw.Start ();
            for ( int i = 0; i < 1001; i++ )
            {
                CreateComplement ( bmp );
            }
            Console.WriteLine ( sw.Elapsed );
            bmp.Save ( "blizz2001-1024 inverted.bmp" );
        }

        static void CreateComplement ( Bitmap bmp )
        {
            BitmapData bmpData = bmp.LockBits ( new Rectangle ( 0, 0,
bmp.Width, bmp.Height ), ImageLockMode.ReadWrite, bmp.PixelFormat );
            unsafe
            {
                byte* ptr = (byte*)bmpData.Scan0;
                int offset = bmp.Width * 3 - bmpData.Stride;

                for ( int i = 0; i < bmpData.Height; i++, ptr+=
offset  )
                {
                    for ( int j = 0; j < bmpData.Width; j++, ptr+=3)
                    {
                        ptr[0] ^= 255;
                        ptr[1] ^= 255;
                        ptr[2] ^= 255;
                    }
                }
            }
            bmp.UnlockBits ( bmpData );
        }
    }
}

Output:
00:00:01.6308407

Your way, I gain about a second and a half.

I'll fix my VB.NET code latter.

--
Tom Shelton
Author
29 Nov 2007 6:28 PM
Twayne
Michael C wrote:
> For everyone who hasn't been following the other exciting thread this
> was the challenge. A comparison between the speed of VB6 and dot net.
> The challenge is to invert every pixel of a bitmap pixel by pixel.
....
>  Michael

I've been following the threads as I got the time and have to admit to being
a tad disappointed, since later discussion seems to indicate they weren't
acually pure VB.net vs. VB6 languages used to get the results.  And I don't
understand the C inclusion at all.
   I was going to ask for some clarifications but I guess on second though
it would be better to consider my comments as rhetorical, so call it my 2
cents as an outsider instead.  And a certain amount of education for myself.

Regards,

Pop`
Author
29 Nov 2007 8:58 PM
Tom Shelton
Show quote
On Nov 29, 11:28 am, "Twayne" <nod...@devnull.spamcop.net> wrote:
> Michael C wrote:
> > For everyone who hasn't been following the other exciting thread this
> > was the challenge. A comparison between the speed of VB6 and dot net.
> > The challenge is to invert every pixel of a bitmap pixel by pixel.
> ...
> >  Michael
>
> I've been following the threads as I got the time and have to admit to being
> a tad disappointed, since later discussion seems to indicate they weren't
> acually pure VB.net vs. VB6 languages used to get the results.  And I don't
> understand the C inclusion at all.
>    I was going to ask for some clarifications but I guess on second though
> it would be better to consider my comments as rhetorical, so call it my 2
> cents as an outsider instead.  And a certain amount of education for myself.
>
> Regards,
>
> Pop`

The VB.NET code was pure .NET.  The C# code was pure .NET.  It's just
that C# allows you to do direct memory accesses, so it has a speed
advantage on a limited number of operations.

--
Tom Shelton

AddThis Social Bookmark Button