Results 1 to 15 of 15

Thread: Writing numbers to file fast! (Need Faster float to char implementation)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    30
    Thanked 13 Times in 12 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    Quote Originally Posted by wysota View Post
    The quickest way to do it is not to convert floats to text
    Do you have any control over the application that is supposed to read the data you write, i.e. could you write the raw bits that make up the double values without converting them to text and have the reader accept them in that format?

  2. #2
    Join Date
    Apr 2008
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    11
    Thanked 7 Times in 3 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    Do you have any control over the application that is supposed to read the data you write
    Yes I have full control, since I am the only user. At the moment I usually import the data into scilab or matlab to do some math, so I can essentially accept anything that isnt encoded in some wierd encrypted format.

    What were you thinking? Using one of the toHex() routines and write that? Since I dont think sprintf has a binary option?
    Best Regards,
    Phil Winder
    www.philwinder.com

  3. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    30
    Thanked 13 Times in 12 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    I was thinking that you could cast a pointer to the first element of your double array to char * and feed it along with the size of the array in bytes to QIODevice::write

  4. The following user says thank you to drhex for this useful post:

    philwinder (5th December 2008)

  5. #4
    Join Date
    Apr 2008
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    11
    Thanked 7 Times in 3 Posts

    Smile Re: Writing numbers to file fast! (Need Faster float to char implementation)

    drhex, pgorszkowski:
    I have now tried your suggestions (you were suggesting the same thing) and they work fantastically. Writing the data to a binary format is many times faster than before.

    The allocation of the array takes about 140ms, which I dont think will get any faster now (although I might try and use QVector to see how that compares - done takes 310 ms). However the more interesting bit is that a QFile::write takes 1 second, whereas the std::ofstream takes 3 seconds. I thought the std function would have been faster, but there you go. Maybe we really are to a point where hard drive speed is the limiting factor.

    By the way, allocating an array then writing the whole array is faster than writing each individual element (1.2 seconds vs 3 seconds) which I guess one would instinctively assume.

    Many thanks to drhex and pgorszkowski and as always, wysota. Cheers.

    P.s.
    For testing and future viewers, here is some cleaned up code with the fastest possible combination:
    Qt Code:
    1. QTime timer;
    2.  
    3. QMessageBox::information(0,"","Ready");
    4. QFile file;
    5. file.setFileName("C:/test.txt");
    6. file.open(QFile::WriteOnly);
    7.  
    8. double f = 0.541263266;
    9.  
    10. int size = 200000*30;
    11. double * af = new double[size];
    12.  
    13. timer.start();
    14. for( int x = 0; x < size; x++ )
    15. af[x] = f;
    16.  
    17. int time = timer.elapsed();
    18. QMessageBox::information(0,"","Elapsed Time: " + QString::number(time) + "ms" );
    19. timer.restart();
    20. file.write((const char*)af, size*sizeof(double));
    21. file.flush();
    22. file.close();
    23. time = timer.elapsed();
    24. QMessageBox::information(0,"","Elapsed Time: " + QString::number(time) + "ms" );
    25. delete [] af;
    To copy to clipboard, switch view to plain text mode 
    Best Regards,
    Phil Winder
    www.philwinder.com

  6. The following user says thank you to philwinder for this useful post:

    Cruz (16th March 2009)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    With these performance statistics on 30 seconds worth of data (1.2 s), it looks like you might be able to write your data in real time, as opposed to storing 30 seconds worth and then writing. Also would give you some protection against data loss if something fails - can you afford to lose 30 seconds worth of data? Writing in real time, you lose at most a second or so.

    Not that you should think in units of seconds in the first place - if you are sampling a continuous data stream at 200kS/s, then -the smallest- time interval at which you can write in real time without data loss would be better than buffering for some human-defined period like 1, 5, or 30 seconds before writing.

    This is where you might make use of a memory-mapped file. If you know how long you will be acquiring data in total (say 10 minutes), then you allocate the memory-mapped file to a size of 200000 * 600 * sizeof ( double ) and simply acquire to that "array" as if it were the double * array you allocate in your example code. The file system takes care of flushing it to disk at appropriate times (or you can do it manually every second or whatever if you don't trust the file system to do it for you).

    With memory-mapping, you can completely ignore all the details of file I/O except those involved in originally opening and then closing the memory-mapped file. Everything else just looks like filling up an array.

    David

  8. #6
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    30
    Thanked 13 Times in 12 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    I notice that you tried earlier to multiply your aquired values with 1000000 to get an integer. How much precision does your data have? Using float rather than integer might be sufficient, letting you store twice as many values or write them faster.

  9. #7
    Join Date
    Apr 2008
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    11
    Thanked 7 Times in 3 Posts

    Default Re: Writing numbers to file fast! (Need Faster float to char implementation)

    Hi guys,
    I have been using the previous routine to save data now for quite a few months, and everything seems to be fine in Real time.

    @d_stranz: Ok, but the reason I had to move to a file anyway was because my system could only handle a minutes worth of data in memory. Since there are 4 channels, sampling at 200kS/s = 800kS/s * 32 bit for float ~= 24.4mB/s. One minute = 1.5 Gig. A lot of data.

    Also, I cannot find a qt related class that deals with it, so would I have to write my own?

    @drhex: Thats was just because I thought that saving an int would be smaller/quicker. I now know that was wrong. The final piece of code is what I have been using. (Or something similar)
    Best Regards,
    Phil Winder
    www.philwinder.com

Similar Threads

  1. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  2. fast writing of large amounts of data to files
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2007, 17:33
  3. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 17:23
  4. Writing to file at specific
    By safknw in forum Qt Programming
    Replies: 3
    Last Post: 1st December 2006, 12:12
  5. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 20:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.