No announcement yet.

PowerBasic vs C++

  • Filter
  • Time
  • Show
Clear All
new posts

  • PowerBasic vs C++

    This website has various benchmark tests and articles that claim or express that
    PowerBasic compilers faster code than C.. Or- something to that

    Well I created this simple routine that will randomly fill an array with colors
    When the array is filled the program will send the array to a PictureBox
    in VB. The dissapointing part about this routine is that it is incredibly
    slow. If i wrote this routine in C, I think I would get better results..

    Can someone explain to me, why? Or maybe give me some incite on what
    commands optimize during execution... I can't understand why the
    performance is so slow? This is a 16bit DLL compiler, would 32bit improve this?


    Sub RandomizeBMP (ByVal hDC%, ByVal bWidth%, ByVal bHeight%) EXPORT
    Dim x%, y%, color As WORD, acnt As DWORD, ok As DWORD

    TOTALBYTES = bWidth% * bHeight%
    ReDim bmpdata(TOTALBYTES) as WORD

    acnt = 0
    For y% = 0 to bHeight%-1
    color = int(65000 * rnd(1))+1
    For x%= 0 to bWidth%-1
    bmpdata(acnt) = color
    incr acnt
    Next x%
    Next y%

    ok = SetBitmapBits(hDC%, TOTALBYTES*2, bmpdata(0))
    End Sub

    Explorations v3.0 RPG Development System
    Explorations v9.10 RPG Development System

  • #2
    The only parts of that code liable to be slow in any respect are the RND
    function and the call to SetBitmapBits. At that, the RND function uses a
    well-optimized proprietary design that is faster than most.

    It may be that you need to adjust your expectations. The code shown
    should be reasonably efficient. "Slow", well... that depends on what
    you want out of it. It is almost always possible to speed things up.
    In order to find out which parts would benefit most from optimization,
    you'll need to do more detailed investigation.

    Sure, C can do some things faster than PowerBASIC and, of course, vice
    versa. I doubt the choice of language would make any real difference for
    code of this nature. At any rate, this is a PowerBASIC forum, and not a
    place to start a language flame war.

    Would a 32-bit compiler improve the speed? I'd guess it wouldn't make
    much difference in this case. Maybe, as a 32-bit environment is better
    designed for handling large amounts of data: no segmentation to fiddle
    with. Of course, much better graphics APIs are available under 32-bit
    Windows, and there are other advantages.

    Tom Hanlin
    PowerBASIC Staff


    • #3
      PowerBasic is BASIC, with additional power. C++ stands for extended
      Chinese, for most of us, thus the need for PB. For best possible
      speed, use assembler and 32-bit environment. I doubt C++ would be
      much faster here.

      In this case, VB's PictureBox may be slowing down things quite a bit.
      Have you set AutoRedraw to False? Maybe using a STATIC control (Label),
      or BitBlt to a Form directly will give better results? What happens if
      you remove the SetBitmapBits -part and just run the code by itself?

      (forget it - just read the other thread..)

      [This message has been edited by Borje Hagsten (edited March 05, 2001).]


      • #4

        I don't see why this would have to be slower than C++.

        Some ideas:

        RND(a, b) may be faster (haven't tested).

        Try using a pointer to the first array element and increment it, instead of accessing the array every time.

        You don't have to redim if TOTALBYTES didn't change.

        Use LONGs where possible.

        Use an inline random function.


        [email protected]
        [email protected]


        • #5
          No flame war here...

          I don't like C/C++ (I used to love it.) But since I found PB, I've turned
          my back on C all together. he he .. So don't worry guys, I'm 100%
          loyal to PB.

          My intent with this project is to improve my gaming project. Right now
          its written in VB and WinAPI with PowerBASIC optimized routines. I wanted
          to pull away from WinAPI and write a PowerBasic HIGH SPEED Graphic routine..

          *I didn't want the baggage of DirectX*

          I'm very good at modular code, but optimization sometimes depends on
          the compiler. So, thats why I posted. I just expected more speed starting
          out. (All the code is run on a AMD380Mhz) -

          Explorations v3.0 RPG Development System

          Explorations v9.10 RPG Development System


          • #6
            A few months ago, vbpj had a article on use c++ to make your VB
            programs fly. It had the original vb code and the c++ code for
            the check. I stuck the vb code in PBDLL and ran the test with
            c++ and pbDll libs in the vb program. The c++ matched the article
            speed, the vb matched the article speed, and PBDLL ran faster than
            the c++ code by a few points. Then I optimized the code with
            PB built in stuff, and it ran almost twice as fast as the c++ code



            • #7
              I understand what you are saying.. but I can't find what documentation
              explains what these optimize PB features are and how to implement

              Saying that PB has certain features that run better than C++
              does explain what they are.. Could you give a blind developer a
              little direction?

              Explorations v3.0 RPG Development System
              Explorations v9.10 RPG Development System


              • #8
                I think it's more a matter of being familiar with the tools you use. However, one
                of the conveniences of PowerBASIC is all of the built-in functionality. While
                some similar functions may be available in C or C++ runtime libraries, the C
                versions are likely to be coded in C, whereas the PowerBASIC runtimes are crafted
                directly in assembly language by a master programmer. To the extent you can use
                built-in PowerBASIC statements, you'll not only be able to code faster, you will
                often have faster code.

                Tom Hanlin
                PowerBASIC Staff


                • #9
                  An important thing to remember is that the C++ compiler is
                  highly optimized. It has removed the boundary calculations
                  "bHeight%-1" and "bWidth%-1" from inside the For Loop.
                  Furthermore, the y% and x% variables are more than
                  likely assigned to Assembly registers.

                  For y% = 0 to bHeight%-1
                  color = int(65000 * rnd(1))+1
                  For x%= 0 to bWidth%-1
                  bmpdata(acnt) = color
                  incr acnt
                  Next x%
                  Next y%

                  I remember once (long ago) when an unnamed programmer created
                  a For loop using an un-optimized C compiler that looked like:

                  For (i=0; i < strlen(MyString); i++)

                  He wanted to know why the processing loop got exponentially slower
                  and slower as MyString grew in size. Once the proper correction
                  was made, loop performance shot up by over a factor of 10.

                  Even with optimized compilers, I still do common sense manual
                  optimizations when I code.

                  I'm sure there are other reasons but these are just a couple
                  of examples.




                  • #10
                    PowerBASIC does these things too, of course...

                    Tom Hanlin
                    PowerBASIC Staff


                    • #11
                      Its been by experience that optimising compilers may fix some of
                      the problems with bad code but they will never make really fast
                      good code.

                      Hand optimised assembler has the performance edge over machine
                      optimised code and when a compiler is built in this manner, its
                      speed averages are generally better.

                      Runtime library comparisons fail in benchmark testing because of
                      the enormous variation between routines but loop optimisation is
                      another matter, hand optimised loops run with the minimum overhead
                      and the maximum performance.


                      [email protected]

                      hutch at movsd dot com
                      The MASM Forum - SLL Modules and PB Libraries



                      • #12
                        It's not the (surprise!) source language, it's the compiler.

                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]