Page 1 of 3 123 LastLast
Results 1 to 40 of 83

Thread: vb vs c++ (graphics programming)

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    vb vs c++ (graphics programming)

    Hi,

    I have a routine in VB that reads a text file (random access) full of drawing coordinates and then draws everything onto a picturebox on my form.

    Since the text files are usually larger than 100 MB in size, this process takes a long time, usually over 3 minutes!!!

    I'm wondering, how much faster would the same thing take in c++? If there is a significant difference then I may be forced to switch over since 3 minutes is unacceptable for loading this image onto the picture box. More specifically, how does c++ file access differ from VB in speed, and how does c++ drawing methods differe from VB in speed?

    Thanx!

  2. #2
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    I might not be a C++ expert but file access (Reading & Writng) speed differs significantly! The thing is, it's not just the language! 100 MB? You're practically loading a game level. That takes pretty long no matter what language you will be using! C++ might save you a quarter of the time but it ain't gonna save the bad method you're using. Try revising your graphics format. Other than that, there are code improvements that you can do in Visual Basic or any other language for that matter. For example, try accomplishing multiple steps in the least amount of code. That's just my opinion though! Good luck!

  3. #3
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    Visual Basic is a scripting language, c++ interacts with memory directly, not only would it run faster the whole program would be smaller too

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    File access is no faster in C++....

    But here you have to realize a few things...

    100mb is ALOT of data... i can only assume you meant 100kb...

    if you're reading it in byte by byte, that'll take forever... in which case, the best thing to do, is make a buffer and GET the data in one line, not in a loop... it will gain you a 85%+ increase in speed on large files.

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by Mushroom Realm
    Visual Basic is a scripting language, c++ interacts with memory directly, not only would it run faster the whole program would be smaller too
    VB is a compiled language. not scripting language. the code is not encrypted or anything like that when compiled. it is made into a true win32 executable.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    As an example, the code listed on this linked page reads a 10mb text file in 250milliseconds on a 850mhz computer.

    http://www.vbforums.com/showthread.p...s&pagenumber=2
    Last edited by nemaroller; Sep 14th, 2002 at 01:29 PM.

  7. #7

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ok,

    The 'data' file is 95.3 MB (MB not Kb)!!

    This is comprised of probably 500000 or more records, each of which contains 2 x and y coordinates, the colour of the line/arc/circle, and the line thickness.

    This data is for a large and very detailed map of a large city. It's used by utility companies. We have a program here called CableCad that can view this map in less than a minute. In addition to this when zooming in/out after the image has been drawn the first time, the image loads incredibly fast.....faster than any other program out there. Unfortunately, CableCad does not offer all the features we think are critical for this application (I won't go into this in more detail).

    What my program does, is it opens up a dxf file (which has been generated from within CableCad) that is usually over 300 MB in size (dxf files are HUGE). My program uses a 'process dxf routine' function which takes the dxf file, extracts only the data I need in order to draw everything onto the map, organizes the data into different drawing 'objects' (for example, all lines are sent to 1 file, all circles to another file, all arcs to another file, all text to another file).

    The idea is that the process dxf routine is only executed maybe every week (since the original files are updated about every week). By processing the dxf file like this, we can speed up the loading time tremendously.

    I've included a screen shot of the picture box with the map loaded.

  8. #8

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ok so here is the image
    Attached Images Attached Images  

  9. #9

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Hmm,

    So would I be better off to read in say 1000 records at a time? Currently I just read in a record at a time. I guess since each record is the same size (type object) I could do this...?

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, you could stop at 1000, but I would just load the whole thing into memory... in one fell swoop...


    However, I have a few questions:

    1) You say you have a text file with these coordinates and related info, but then you mentioned you are using Random Access methods to get this info.

    Well, Binary Access would greatly speed this up...
    So, if its a binary file, its random access...
    If not, its more likely a sequential text file...

    I imagine you are really talking about a sequential text file, since this was generated by another program, supposedly for export to other applications.

    Please get back to us on which one it is...

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    One more thing, if you are seriously about creating images in detail like the one you posted, you better be able to do DirectX programming...

    If you are using basic operations such as PSet and Line, well, and it is taking you 3 minutes to create an image, then you will have to reogranize your code, or move to DirectX.

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    As an example, using native Line method of VB, the following code makes 500,000 lines in 4 secs on my Athalon 850mhz machine...

    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3. Dim i As Long
    4. Picture1.Scale (0, 0)-(7000, 7000)
    5. Dim cordarray(500000, 4)
    6. For i = 1 To 500000
    7. cordarray(i, 1) = Int((7000 - 1 + 1) * Rnd + 1)
    8. cordarray(i, 2) = Int((7000 - 1 + 1) * Rnd + 1)
    9. cordarray(i, 3) = Int((7000 - 1 + 1) * Rnd + 1)
    10. cordarray(i, 4) = Int((7000 - 1 + 1) * Rnd + 1)
    11. Next
    12. MsgBox ("array populated")
    13. For i = 1 To 500000
    14. Picture1.Line (cordarray(i, 1), cordarray(i, 2))-(cordarray(i, 3), cordarray(i, 4)), 100
    15. Next
    16. MsgBox ("done")
    17. End Sub

  13. #13

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I simply can't load the entire contents of the file into memory at once since not many people seem to have 100 Mb of Ram.

    With regards to the type of file access I am using

    Im not sure what its called but this is how I open and save data to the files

    VB Code:
    1. 'Line object
    2. Public Type mapLineType
    3.     XPosStart As Single
    4.     YPosStart As Single
    5.     XPosEnd As Single
    6.     YPosEnd As Single
    7.     Colour As Long
    8.     Thickness As Single
    9. End Type
    10. Private mapLine as mapLineType
    11.  
    12. 'How I open the files
    13. Open strFile For Random As #fileNum Len = Len(mapLine)
    14.  
    15. 'How I save data to the files after filling the mapLine object with data
    16. Put #fileNum, gLineCount, mapLine

  14. #14

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I think the majority of the 3 minute load time is spent accessing the file and/or refreshing the image as I draw stuff on to the screen. To minimize the # of refresh calls I only refresh the image every 10000 lines.

  15. #15
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    Look up Binary File Access. There are plenty examples in these forums.

  16. #16
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    BTW...I have a small idea to speed up the loading significantly. Make the image invisible as you load it and then display it all in the end! That will dramatically speed up loading time. Just an idea but if you don't intend to do it that way, it's your loss. Kuz the system will take plenty of time to display and refresh the image time and time again and that will get in the way of the loading procedure.

  17. #17

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I can't do that.

    I need to be able to show the image loading as it loads, since the user can choose to cancel loading at any time, or zoom in before the loading process is complete.

  18. #18

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    WIth binary access, can you read a 'section' of data directly into a type variable, such as with random access?

    This to me seems like a great benefit and one that would speed things up considerably. Otherwise you would have to read in data, then break it apart into separate variables etc.

  19. #19
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Originally posted by ae_jester
    I think the majority of the 3 minute load time is spent accessing the file and/or refreshing the image as I draw stuff on to the screen. To minimize the # of refresh calls I only refresh the image every 10000 lines.
    If there are over 500,000 lines, and you are refreshing every 10,000 lines, then that is also a major speed factor. If I were you, I would only draw it once at the end, and just update a progress bar or something to let the user know it's still loading.
    <removed by admin>

  20. #20
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, Windows uses page files, so 100mb isn't a problem....

    As far as your file access, that is random access... and you can read it with binary access...

    If memory serves me correct, Single Types take 2 bytes..., longs take 4... so the LenB of your mapLineType should be 2*5=10 + 4= 14 bytes long.

    So in a byte array, you'll have 14 bytes for each record.
    Now, before moving ahead, the code you posted, you note you are saving the lines back to file... which means you are editing the data? and then resaving those lines to file?

    You will need to give more info before I recommend moving to binary access... because with Binary, you must perform conversions, and depending on if you have to save or edit the data, it can make a big difference on how to approach it code wise.

  21. #21

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    At one point in time I considered drawing the image to a device context in memory and then just copying this to the picture box every few seconds. I thought this might be faster, but I never got anywhere with it because of a number of scaling problems and lack of experience working with these.

  22. #22

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Sorry my previous post obviously confused the hell outta you.

    I only write to the files during the 'process dxf' routine, which is not really related to the loading process at all.

    On the actual map screen I only read the files, and never need to write back to them.

  23. #23

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    If there are over 500,000 lines, and you are refreshing every 10,000 lines, then that is also a major speed factor. If I were you, I would only draw it once at the end, and just update a progress bar or something to let the user know it's still loading.
    Actually I tried refreshing every 40000 lines and the time it took to load was very similar to refreshing every 10000 lines.....So I'm pretty sure the file access is the main cause of the speed problems.

  24. #24
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    if you run my code example on 500,000 lines, you can see it only takes VB 4-5 seconds (Depending on your processor) to create all those lines. So it is definitely the file access aspect.

  25. #25
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Here's what I suggest as an example...
    VB Code:
    1. Option Explicit
    2. Private Sub Command1_Click()
    3. Dim i As Long
    4. Picture1.Scale (0, 0)-(7000, 7000)
    5. Dim cordarray(500000, 4)
    6.  
    7. For i = 1 To 500000
    8. cordarray(i, 1) = Int((7000 - 1 + 1) * Rnd + 1)
    9. cordarray(i, 2) = Int((7000 - 1 + 1) * Rnd + 1)
    10. cordarray(i, 3) = Int((7000 - 1 + 1) * Rnd + 1)
    11. cordarray(i, 4) = Int((7000 - 1 + 1) * Rnd + 1)
    12. Next
    13.  
    14. Open ("C:\testfile.tst") For Binary As #1
    15. Put #1, 1, cordarray
    16. Close #1
    17.  
    18. MsgBox ("array populated and saved")
    19. Open ("C:\testfile.tst") For Binary As #1
    20. Get #1, 1, cordarray
    21. Close #1
    22. For i = 1 To 500000
    23. Picture1.Line (cordarray(i, 1), cordarray(i, 2))-(cordarray(i, 3), cordarray(i, 4)), 100
    24. Next
    25. MsgBox ("done")
    26. End Sub

    Now, this populates 500,000 random lines also, and that takes 2 seconds itself, and this would not be a requirement of your program since you are obviously reading from an existing file, so the first part of my example would be unnecessary and irrelevant to the final outcome of speed.

  26. #26

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Could anyone show me how I could do this...

    VB Code:
    1. Open strFile For Random As #fileNum Len = Len(mapLine)
    2.    
    3. numLines = LOF(fileNum) / Len(mapLine)
    4.    
    5. Dim i As Long
    6. 'Display the lines on the screen
    7. For i = 1 To numLines
    8.     Get #fileNum, i, mapLine
    9.     'bla bla bla
    10. Next i
    11.  
    12. Close #fileNum

    ... using binary access? In particular, reading data into a type variable.

  27. #27

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ok thanx for the example.

    Any reason why you didnt declare the array as a certain type?

  28. #28
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Any type is composed of bytes underneath it all...

  29. #29

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I'm not sure if arrays will work for me for this.

    My lineType object looks like this...

    Public Type mapLineType
    LineType As Byte
    XPosStart As Single
    YPosStart As Single
    XPosEnd As Single
    YPosEnd As Single
    Colour As Long
    Thickness As Single
    End Type

    So with an array all elements are of the same type right?
    So could your example be modified to use something like the above? Or would I have to treat everything as Single's since the coordinates themselves can be decimal numbers?

  30. #30

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Any type is composed of bytes underneath it all...
    What? I dont understand what you mean...

  31. #31

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Ok I figured it out

    Thanx for your help

    I'll let you know how much faster it goes!

  32. #32

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    What is the maximum size of an array of types?

    I have this line

    Dim lineTypes(1000) as mapLineType

    where mapLineType is described in an above post

    If I try setting the array to 2000 nothing happens. Now when I add error checking code in it says "Overflow".........but without the error code there i get no error message, just nothing at all happens!

  33. #33
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Jester, ill help you out in a few hours... i gotta go do somethin with wife...

  34. #34
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You shouldnt be having a problem with an array of 2000.

    In fact, you should be able to easily go to 1 million...

    I'll need to see your code and how you've implement the suggestions so far, so attach the code in your post...

  35. #35

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I won't be back at work until next Wednesday. Maybe I can post the code tomorrow as I have access to it from home.

  36. #36
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, here you go.... the file access (load) part is faster than my original byte array version... which I can't explain, so don't bother asking...

    You'll note the bold line in the code below... this line decreases the speed at which the program fills the screen to 11 seconds from 4.5 seconds on my machine... for some reason changing .DrawWidth property of the picturebox incurs a heavy performance hit.... this is where writing directly to video card memory(DirectX) would prove useful... especially when zooming the image... I would ask some people in the Graphics programming section of this forum...

    I add a DoEvents after the bolded line to allow for user interaction, such as moving the form, or perhaps for a cancel button. DoEvents only added 1.5 seconds to the overall time on my machine.

    VB Code:
    1. Option Explicit
    2. Private Type mapLineType
    3.     XPosStart As Single
    4.     YPosStart As Single
    5.     XPosEnd As Single
    6.     YPosEnd As Single
    7.     Colour As Long
    8.     Thickness As Single
    9. End Type
    10. Private mapLine(500000) As mapLineType
    11.  
    12. Private Sub Command1_Click()
    13. Dim i As Long
    14. Picture1.Scale (0, 0)-(7000, 7000)
    15.  
    16. 'create some random test numbers
    17. For i = 1 To 500000
    18. mapLine(i).XPosStart = Int(7000 * Rnd + 1)
    19. mapLine(i).YPosStart = Int(7000 * Rnd + 1)
    20. mapLine(i).XPosEnd = Int(7000 * Rnd + 1)
    21. mapLine(i).YPosEnd = Int(7000 * Rnd + 1)
    22. mapLine(i).Colour = Int(31000 * Rnd + 1)
    23. mapLine(i).Thickness = Int(2 * Rnd + 1)
    24. Next
    25.  
    26. 'save test numbers to file
    27. Open ("C:\testfile.tst") For Binary As #1
    28. Put #1, 1, mapLine
    29. Close #1
    30.  
    31. MsgBox ("array populated and saved")
    32. 'load in test numbers
    33. Open ("C:\testfile.tst") For Binary As #1
    34. Get #1, 1, mapLine
    35. Close #1
    36. MsgBox ("loaded")
    37.  
    38. 'draw test numbers
    39. For i = 1 To 500000
    40. [b]Picture1.DrawWidth = mapLine(i).Thickness[/b]
    41. Picture1.Line (mapLine(i).XPosStart, mapLine(i).YPosStart)-(mapLine(i).XPosEnd, mapLine(i).YPosEnd), mapLine(i).Colour
    42. DoEvents
    43. Next
    44. MsgBox ("done")
    45. End Sub
    Last edited by nemaroller; Sep 15th, 2002 at 11:14 AM.

  37. #37
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, i've been playing around and here's a few things you should know....

    Set your picturebox1.AutoRedraw=False
    Set your picturebox1.ClipControls = False

    This allows for a great improvement in speed...

    I have included a form for download that you can run , which compares the intrinsic VB Line methods, versus the Win32 API (C functions) LineTo methods... as you will see, both run at the same speed regardless, other than you don't have to set picturebox1.autoredraw or clipcontrols=false to realize that speed for API version. Plus when compiled, the API version seems to work better as far as retaining the image made, since you haven't had the need to adjust the clipcontrols or the autoredraw properties.
    Attached Files Attached Files
    Last edited by nemaroller; Sep 16th, 2002 at 05:23 PM.

  38. #38
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Btw, also tried a version of this in VB.NET, well, it actually ran slower than the VB6 version....

  39. #39

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I actually made a mistake

    There isn't 500,000 records there is close to 4 million!

    On this computer it takes 37 seconds just to open the file and plug the data into the array!!!! This isnt much good especially when the user wants to zoom into a small region. I tried opening the file in 'sections', for example 100000 records at a time (anything larger than this causes annoying delays during the drawing process), which did work but really didn't improve the overall time from before by a big margin.

    There is no way I can set the AutoRedraw property to false, because then if another window is moved over the image, or the user switches to another app, the image dissapears when they come back. Unless there is some method of avoiding this???

    I'm going to try the API method and see if that does much

  40. #40
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well well...

    Is it faster than the commercial product you use to load images? And if not, is that running on a different computer with higher processing speed? (Some workstations have dual processors)...

    as I said above, the API method will retain the image properly...
    Last edited by nemaroller; Sep 18th, 2002 at 03:27 PM.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width