Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
F

frylord

@frylord
About
Posts
8
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • String concat slow despite Stringbuilder
    F frylord

    Using this code: Dim Rnd As Random = New Random() Dim Dbl_RndData(65535) As Double Dim Str_RndData As StringBuilder Dim i As Integer = 0 Dim StpWtch As Stopwatch = New Stopwatch Str_RndData = New StringBuilder() ' Fill the Double array with random data For i = 0 To 65535 Dbl_RndData(i) = Rnd.NextDouble Next StpWtch.Start() ' Convert the data to a string using stringbuilder For i = 0 To 65535 Str_RndData.Append(Dbl_RndData(i).ToString("0.0000")) Str_RndData.Append(", ") ' Append a comma to get CSV format Next StpWtch.Done() TextBox1.Text = StpWtch.ElapsedTime.ToString And this Stopwatch counter: http://www.codeproject.com/KB/vb/vbnetstopwatch.aspx[^] I get 0.196240863014713 seconds

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    The manual isn't really clear on this but by trying I figured out that the device needs 4 decimals after the comma to get an accurate output. For example when I set the Vmax to 9 volts and I want it to output 3 volts (1/3) then I need to set the data point to 0.3333333333333333333 if I had infinite accuracy. But just by trying I found out that by setting the data point to 0.33 i gives me 2.7 volts and when I set it to 0.3333 it gives me 3.01 volts. I think you might be onto something with the GUI thing. To be honest my first version wasn't with stringbuilder I think. When I first wrote the code I put it under a button using normal string. Then when I found that was too slow I built the thread thing then later on I put stringbuilder in there. Okay after some trying I found that the culprit in this case was the progressbar which was being updated every loop. When I just use the stringbuilder it's quite fast. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Rnd As Random = New Random() Dim Dbl_RndData(65535) As Double Dim Str_RndData As StringBuilder Dim i As Integer = 0 Str_RndData = New StringBuilder() ' Fill the Double array with random data For i = 0 To 65535 Dbl_RndData(i) = Rnd.NextDouble Next ' Convert the data to a string using stringbuilder For i = 0 To 65535 Str_RndData.Append(Dbl_RndData(i).ToString("0.0000")) Str_RndData.Append(", ") ' Append a comma to get CSV format Next End Sub Thanks for the help.

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Well to be honest I don't know. Somehow my college teachers indoctrinated the idea that you should block your current thread so other threads can get some execution time aswel. The same idea continued into my university years and has sticked with me ever since and I've also seen the same in alot of online tutorials and examples. I think this idea of blocking one thread to give others a chance could be left over from the days when schedulers weren't as complex.

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Hi Luc, Once again thank you. The device is connected through a TCP/IP connection. The thing is that the SDK doesn't allow for sending the data one by one, you need to send it in one line. So what I do is build the string I need to send using string builder and then afterwards send it using the SDK function and the stringbuilder ToString function. I think I'll have to explain a bit more about the device, atleast I owe you that much I guess. The so called "device" is a arbitrary waveform generator. here's an excerpt from the manual.

    You can download from 1 point (a dc signal) to 65,536 (64K) points per
    waveform. You can download the points as floating-point values. Use the SendArbData function to
    download floating-point values from -1.0 to +1.0.

    And here's the bit on how the data should be.

    The following statement shows how to use the SendArbData function to
    download seven points to volatile memory.
    SendArbData("1, .67, .33, 0, -.33, -.67, -1")

    So basically it wants me to format a huge string of 65536 data points in CSV format and then use the SendArbData function to send it to the device.

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Hi Paul, It's not the sleep(1). The reason I know this is at first I expected this process to complete within a "reasonable" (not letting the user think the GUI got stuck) amount of time so I did this process under a button. Then when I noticed it's taking a lot of time to complete I put it in a thread with a nice GUI and such. The sleep(1) is just there to force the scheduler to switch threads to give other threads some execution time as well. Thanks for the effort.

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Hi Jasey9, I have a few other threads running and the sleep(1) is just there to force the scheduler to switch threads so this one doesn't hog up all the execution time. Give other threads a chance so to speak. :) Thanks I'll try your suggestion.

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Hi Luc, Thanks for your effort. 1. The sleep(1) is there todo just that, to give other threads a chance to get some execution time in the scheduler. 2. I didn't measure it using any timers, just the watch on my wrist. :) The thing is I did build a nice little GUI around this whole process with a nice animation and a progressbar but it still seems weird to me that this whole process would take so much time to complete. 3. You might be onto something there, how do I preallocate the size of a stringbuilder?

    Visual Basic data-structures performance question

  • String concat slow despite Stringbuilder
    F frylord

    Hi, I'm not a VB 2005 coder. I'm forced to use VB because the SDK that came with a device only supports VB. I have to send 65536 floating values to this device. I store these values in a double array and when I need to send them to the device I use stringbuilder in a thread to build the string, as following.

        For i = 0 To 65535
            System.Threading.Thread.Sleep(1)
    
            ArbString.Append(", ")
            ArbString.Append(ArbData(i).ToString("0.0000"))
            worker.ReportProgress(i)
        Next
    

    Despite using stringbuilder the whole takes around 40 seconds to complete. Am I doing something wrong here or is there something I can do to speed the whole thing up? TIA

    Visual Basic data-structures performance question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups