CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2004
    Posts
    481

    Fast way to convert string array to double array...

    Hi GURU,

    What is the fastest way to convert string array to double array? Currently, I am doing this:
    Code:
    ...
    ...
    ...
    double[] arrDouble = new double[arrString.Length];
    for(int i=0; i<arrString.Length; i++)
    {
       arrDouble[i] = double.Parse(arrString[i]);
    }
    ...
    ...
    ...
    This is very slow for big array (about 3 millions).

    Any idea?

    Thanks GURU for any help.

    Cheers

  2. #2
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Fast way to convert string array to double array...

    Maybe the Convert class works a little bit faster.
    Code:
    ...
    ...
    ...
    double[] arrDouble = new double[arrString.Length];
    for(int i=0; i<arrString.Length; i++)
    {
       arrDouble[i] = Convert.ToDouble(arrString[i]);
    }
    ...
    ...
    ...
    Useful or not? Rate my posting. Thanks.

  3. #3
    Join Date
    Oct 2004
    Posts
    481

    Re: Fast way to convert string array to double array...

    Hi Torrud,

    Thanks for replying.

    I just tested the code, it doesn't give me any improvement

    Here is the speed comparison to convert object.
    http://blogs.msdn.com/bclteam/archiv...11/371436.aspx

    Please note:
    - The link I sent is the comparison to convert object to double.
    - You will see double.Parse is very slow because it actually has to convert the object to string.
    - It is not the case here because my array is already a string.
    Any other idea? Thanks again for any help.

    Cheers

  4. #4
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Fast way to convert string array to double array...

    Here is the speed comparison to convert object.
    The information there is obviously at least three years stale.

    Doing a series of simple tests with v2005, my results were that Double.TryParse was a few percent faster than any of the others. Since it is also the safest, it seems like the obvious choice.

    This is very slow for big array (about 3 millions).
    Of course! That's a fact-of-life with floats.

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Fast way to convert string array to double array...

    uh, so why are you converting the value of the string to a double? .net strings are 16 bit values, so ushort would be a better choice there, so I'm just curious why you'd use such a large data type for that?

  6. #6
    Join Date
    Oct 2004
    Posts
    481

    Re: Fast way to convert string array to double array...

    Hi Zips & MadHatter,

    Thanks for your kind reply.

    I have a large data (about 3GB per file). This file is just a simple Comma Delimited file that is generated by our legacy system (quite an old system). Now, I need to read all of this data and do some calculation on it.

    The first 3 columns are strings and the rest of the columns are doubles.
    The number of columns can be vary between 10000-1000000 depending the total number of attributes they have.

    The problem I have is reading these files is too slow. In normal operation, I need to read about 5-10 of this files. And yes, I am using 64bit machine.

    If there is nothing I can do, that is ok. I just thought you guys might have a better algorithm to use

    Any suggestions?

    Cheers

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Fast way to convert string array to double array...

    parsing numbers out of a CSV, and obtaining an array of doubles based on each character's ASCII or Unicode character value is two (oh so) totally different things...

    if you're trying to parse in a CSV file, then you DO NOT iterate over the file contents char by char..., you split it on commas, and parse in the different segments...

    what you need is something like this: http://www.codeproject.com/KB/database/CsvReader.aspx

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Fast way to convert string array to double array...

    Quote Originally Posted by ryu
    The problem I have is reading these files is too slow
    And how do you know that the bottleneck is in the double parsing?

    Any suggestions?
    Well, youre reading 3 gigs of data. Did you think it was gonna be fast? I have a process that reads and uploads an 80 meg customer file into an oracle database, and using bulked transfer methods, the fastest I can get it to complete is about 4 minutes - I think thats pretty good for 800,000 records considering all the validation, and overheads..

    How "slow" is your process and how much "slower" is it than it needs to be?

    I suggest you do some more tests before absolutely declaring that the double parsing is the slowest bit
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Oct 2004
    Posts
    481

    Re: Fast way to convert string array to double array...

    Hi cjard,

    I am not sure how does it done in Oracle, but I would imagine that there is a lot of overhead to insert these records in the database. But my problem isn't about inserting the data to the database. I basically wanted to know if there is anything I can do to improve loading speed.

    Here is my original test result.
    Reading without double.parse takes about 1 minute and 30 seconds.
    Reading with double.parse takes about 8 minutes and 50 seconds.
    I suspect it is really fast without double.parse is because C# does the optimisation and ignoring the for...loop because it doesn't been used anywhere.

    So, in order to get a real value, what I did is to put Console.Write() to print out each of the value out. So, I modified the codes to:
    Code:
    ...
    ...
    ...
    double[] arrDouble = new double[arrString.Length];
    for(int i=0; i<arrString.Length; i++)
    {
       arrDouble[i] = double.Parse(arrString[i]);   //I will comment this line when testing without double.Parse
       Console.Write(arrString[i]);
    }
    ...
    ...
    ...
    But Unfortunately, running the 3GB is extremely slow. So, I modified my input file to smaller file (just about 150MBytes --> 60 fields + 645,000 lines.)

    Here is the new result:
    Reading without double.parse takes about 7 minutes and 55 seconds
    Reading with double.parse takes about 9 minutes and 56 seconds
    As you can see, even with the small file (150 Mbytes), it takes 2 minutes longer. You may argue that it is a small number and I totally agree with you. All I wanted to know if there is a faster way to parse the double so I can increase the loading speed. If not, that is ok because I can live with it.

    Cheers

    Btw, please don't be surprise with the number. Console.Write() is really slow. If you gonna ask me how I know Console.Write is slow, well, please google it or simply create a new Thread in CodeGuru and I am sure a lot of people will response to that.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Fast way to convert string array to double array...

    You need to SPLIT() the data based on the delimiter, into array or classes, whichever you prefer. Not character based.

    Read a chunk, read up to the next record start, then parse the chunk.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Oct 2004
    Posts
    481

    Re: Fast way to convert string array to double array...

    Hi Dglienna,

    I believe that is what I did.
    Code:
    ...
    ...
    ...
    string [] arrString = st.Split(new char[] {','});
    double[] arrDouble = new double[arrString.Length];
    for(int i=0; i<arrString.Length; i++)
    {
       arrDouble[i] = double.Parse(arrString[i]);
    }
    ...
    ...
    ...
    Is that ok?

    Cheers

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

Featured