Results 1 to 13 of 13

Thread: slow variables!

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    slow variables!

    when filling a string with alot of chars in a loop, it starts to get really slow around 30000 chars....is there a faster way? i really need like a mb in the string! can i write it directly to the hd?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    23
    When you say filling a string, are you iteratively appending a character to the end of a string or are you placing characters into a fixed length string?

    If you're just doing myString = myString & theCharacter then performance will eventually hit the floor. You will be wanting it as a fixed length string and then insert the charcter into the string at the place you want it.

    The problem with using myString = myString & theCharacter is that VB will allocate a new string every single time it's executed. Each time the line is executed, VB will have to keep the contents of myString, allocate a new string (of size myString+1), copy the contents of myString into it and then append theCharacter onto it. This new string is assigned to myString and then the original myString is released.

    So, as the size of myString goes up, performance goes down.

    What is it you're using the string for? 1MB is an awful lot of text ...

    If you are looking to write it directly to the hard disk, the "Microsoft Scripting Runtime" component is a nice easy one to use.

    Dave

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Open File For Output (look in MSDN for specific syntax)
    Then Write/Print to the file
    Close file.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    You might want to look here .
    A disadvantage is that you have to keep track of the length of the string, since it is used as the position for the next append.

    I have modified the code as following, so it isn't in a class anymore and that there are no global variables.

    VB Code:
    1. Public Const STRING_INCREASE = 50000
    2.  
    3. Public Sub AppendString(psDest As String, psSrc As String, plPos As Long)
    4.  
    5.   Do While plPos + LenB(psSrc) > LenB(psDest)
    6.     psDest = psDest & Space(STRING_INCREASE)
    7.   Loop
    8.   CopyMemory ByVal StrPtr(psDest) + plPos, ByVal StrPtr(psSrc), LenB(psSrc)
    9.   plPos = plPos + LenB(psSrc)
    10.  
    11. End Sub

    (It was also more convenient to increase the string_increase size, which is fixed to 10000 in the example)

    The performance boost was great, even for several 100 k!

  5. #5

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    im not really sure how to use that code...
    in a loop i have this code:
    VB Code:
    1. PicString = PicString & Chr(TempRed) & Chr(TempGreen) & Chr(TempBlue)

    how can i change this using that code?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    can anyone help me with this please?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  7. #7

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    please! now that i've got the funktion please tell me how to use it!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  8. #8

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    come on! i know you can do it!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Okay hang on. Don't get your knickers in a twist.
    I'll take a look at it.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i just found out how to do it using the class file instead!
    it works great now! thanks alot!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  11. #11

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    the finished program can be found from the link under my posts...
    it saves pictures in a new fileformat that's ~1kb smaller than bmp

    not very useful, but it was fun making it!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  12. #12
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Sorry, I didn't visit VBForums the rest of the day. There is too much work to do
    Glad to see that the class works.

  13. #13

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    yes...thanks alot for your help!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

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