Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile save for usb drive
Qt 6.11 is out! See what's new in the release blog

QFile save for usb drive

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 15.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S samdol

    Hi,
    I used the following snippnet to save myData to a file. It works well when I save it to my hard disk c:. but when I try to save it to usb drive, it is extremly slow. Is there any way to improve this performance?

            QFile file( myfile );
            if( !file.open( QIODevice::ReadWrite ) )
                return false;
            QDataStream stream( &file );
            stream.setVersion( QDataStream::Qt_4_2 );
            qDebug()<<" takes long for usb drive";
            stream >>myData;
            file.close();
    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #2

    @samdol said in QFile save for usb drive:

    stream.setVersion( QDataStream::Qt_4_2 );

    Do you really need that old version?

    Just as an experiment, remove that line altogether and see if it improves (I really doubt it will but worth a try)

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    S 1 Reply Last reply
    0
    • VRoninV VRonin

      @samdol said in QFile save for usb drive:

      stream.setVersion( QDataStream::Qt_4_2 );

      Do you really need that old version?

      Just as an experiment, remove that line altogether and see if it improves (I really doubt it will but worth a try)

      S Offline
      S Offline
      samdol
      wrote on last edited by
      #3

      @VRonin
      Thanks ,I have replaced it with Qt_5_6. But it is still the same. It takes long time to save on usb drive.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        What USB device is it ?
        What generation of USB do you have available ?
        What is the size of the data that you are trying to write ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          What USB device is it ?
          What generation of USB do you have available ?
          What is the size of the data that you are trying to write ?

          S Offline
          S Offline
          samdol
          wrote on last edited by samdol
          #5

          @SGaist

          The USB device is a normal standard usb2.0 stick drive and I am trying to save 200KB on it. Compare to internal hard drive, it is tool slow.

          I also found that QDir::entryList() is very slow for the directory in usb drive. Is there any workaround at least for windows?
          QStringList dir_list = qdir.entryList( QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden |QDir::AllDirs);
          QStringList fn_list = qdir.entryList( QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden | QDir::Files);

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #6

            Hi
            Usb 2.0 is really slow compared to sata III (and II) that is typically used for hard drives.
            (hence usb 3 was invented)

            https://superuser.com/questions/317217/whats-the-maximum-typical-speed-possible-with-a-usb2-0-drive

            So have you tried to copy a large file to the usb stick and watch the MB/s ?
            If you land between 30-40 MB/s that it not the stick itself.

            When you say "too slow" , does it takes seconds or how slow ?

            Also make sure there is no scanner examining the file in the background.

            You can also play around with this

            #include <stdio.h>
            #include <chrono>  // for high_resolution_clock
            
            void MainWindow::on_pushButton_released() {
            
              const int FILE_SIZE = 200; //size in KB
              const int BUFFER_SIZE = 1024;
              char buffer [BUFFER_SIZE + 1];
              int i;
              auto start = std::chrono::high_resolution_clock::now();
              for(i = 0; i < BUFFER_SIZE; i++)
              { buffer[i] = (char)(i % 8 + 'a'); }
              buffer[BUFFER_SIZE] = '\0';
            
              FILE* pFile = fopen ("g:\\somefile.txt", "w");
              for (i = 0; i < FILE_SIZE; i++)
              { fprintf(pFile, buffer); }
            
              fclose(pFile);
              auto finish = std::chrono::high_resolution_clock::now();
              std::chrono::duration<double> elapsed = finish - start;
              qDebug() << "Elapsed time: " << elapsed.count();
            }
            
            

            I get around 0.2 - 0.3 for saving 200KB file on my usb 2 port with usb 2 device.
            Note that timing file writes using chrono is imprecise but could give hints.

            S 1 Reply Last reply
            3
            • mrjjM mrjj

              Hi
              Usb 2.0 is really slow compared to sata III (and II) that is typically used for hard drives.
              (hence usb 3 was invented)

              https://superuser.com/questions/317217/whats-the-maximum-typical-speed-possible-with-a-usb2-0-drive

              So have you tried to copy a large file to the usb stick and watch the MB/s ?
              If you land between 30-40 MB/s that it not the stick itself.

              When you say "too slow" , does it takes seconds or how slow ?

              Also make sure there is no scanner examining the file in the background.

              You can also play around with this

              #include <stdio.h>
              #include <chrono>  // for high_resolution_clock
              
              void MainWindow::on_pushButton_released() {
              
                const int FILE_SIZE = 200; //size in KB
                const int BUFFER_SIZE = 1024;
                char buffer [BUFFER_SIZE + 1];
                int i;
                auto start = std::chrono::high_resolution_clock::now();
                for(i = 0; i < BUFFER_SIZE; i++)
                { buffer[i] = (char)(i % 8 + 'a'); }
                buffer[BUFFER_SIZE] = '\0';
              
                FILE* pFile = fopen ("g:\\somefile.txt", "w");
                for (i = 0; i < FILE_SIZE; i++)
                { fprintf(pFile, buffer); }
              
                fclose(pFile);
                auto finish = std::chrono::high_resolution_clock::now();
                std::chrono::duration<double> elapsed = finish - start;
                qDebug() << "Elapsed time: " << elapsed.count();
              }
              
              

              I get around 0.2 - 0.3 for saving 200KB file on my usb 2 port with usb 2 device.
              Note that timing file writes using chrono is imprecise but could give hints.

              S Offline
              S Offline
              samdol
              wrote on last edited by
              #7

              @mrjj

              I have no problem when I copy file from usb to internal hard disk or internal hard disk to usb stick. Compare to the speed for copying file by windows explorer, copying file by QFile::copy() is not slow. It is slow when I save file to usb stick by QFile/QDataStream. Also when I browse directories, qdir.entryList() shows poor performance compare to explorer or other windows programs. For example if I enter a directory with 10000 files, it takes more than one minute to finish qdir.entryList().

              mrjjM 1 Reply Last reply
              0
              • S samdol

                @mrjj

                I have no problem when I copy file from usb to internal hard disk or internal hard disk to usb stick. Compare to the speed for copying file by windows explorer, copying file by QFile::copy() is not slow. It is slow when I save file to usb stick by QFile/QDataStream. Also when I browse directories, qdir.entryList() shows poor performance compare to explorer or other windows programs. For example if I enter a directory with 10000 files, it takes more than one minute to finish qdir.entryList().

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                @samdol
                ok. so we assume device is ok.
                Did you try test program and compared elapse times?
                0.3 is not slow as such.
                Could be interesting to see where you land.

                What is myData ?
                One big array ?

                update:
                tried

                For /L %i in (1,1,10000) do fsutil file createnew B%i.tmp 65536
                

                gives me 10.000 files on usb stick.
                (unmount / mount before test to avoid cashing)

                #include <QDir>
                void MainWindow::on_pushButton_2_released()
                {
                    auto start = std::chrono::high_resolution_clock::now();
                    QDir qdir("g:/");
                    QStringList fn_list = qdir.entryList( QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden | QDir::Files);
                    auto finish = std::chrono::high_resolution_clock::now();
                    std::chrono::duration<double> elapsed = finish - start;
                    qDebug() << "Elapsed time: " << elapsed.count();
                }
                

                Elapsed time: 1.54938
                So under 2 secs.
                This is win 10. Qt 5.7.

                S 1 Reply Last reply
                1
                • mrjjM mrjj

                  @samdol
                  ok. so we assume device is ok.
                  Did you try test program and compared elapse times?
                  0.3 is not slow as such.
                  Could be interesting to see where you land.

                  What is myData ?
                  One big array ?

                  update:
                  tried

                  For /L %i in (1,1,10000) do fsutil file createnew B%i.tmp 65536
                  

                  gives me 10.000 files on usb stick.
                  (unmount / mount before test to avoid cashing)

                  #include <QDir>
                  void MainWindow::on_pushButton_2_released()
                  {
                      auto start = std::chrono::high_resolution_clock::now();
                      QDir qdir("g:/");
                      QStringList fn_list = qdir.entryList( QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden | QDir::Files);
                      auto finish = std::chrono::high_resolution_clock::now();
                      std::chrono::duration<double> elapsed = finish - start;
                      qDebug() << "Elapsed time: " << elapsed.count();
                  }
                  

                  Elapsed time: 1.54938
                  So under 2 secs.
                  This is win 10. Qt 5.7.

                  S Offline
                  S Offline
                  samdol
                  wrote on last edited by
                  #9

                  @mrjj
                  Thank you for your example.
                  When I run on_pushButton_released() first time, I got
                  Elapsed time: 16.3639
                  but when I run it second time and later
                  Elapsed time: 0.280801
                  When I run on_pushButton_2_released() for directory with 10000 files on usb, the result is
                  Elapsed time: 53.6064
                  quite slow compare to your result. I used windows7 and Qt 5.6.2
                  Does it mean qdir.entryList() does not like my usb drive
                  or should I upgrade to Qt 5.7?

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi
                    Could you try with clean / new usb stick ?

                    Those numbers are pretty high for entryList.

                    Are those files in sub folders?

                    Its hard to say if it is device or driver issue or something with windows.
                    or if it simply is that slow in some cases.

                    If time permits simple install 5.7 along side 5.6 and test if it changes anything.

                    I will also test on a win 7 tomorrow just to be sure.

                    S 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      Hi
                      Could you try with clean / new usb stick ?

                      Those numbers are pretty high for entryList.

                      Are those files in sub folders?

                      Its hard to say if it is device or driver issue or something with windows.
                      or if it simply is that slow in some cases.

                      If time permits simple install 5.7 along side 5.6 and test if it changes anything.

                      I will also test on a win 7 tomorrow just to be sure.

                      S Offline
                      S Offline
                      samdol
                      wrote on last edited by
                      #11

                      @mrjj
                      As you suggested, it seems that could be a problem of Qt 5.6.2.
                      After long wait, finally I could try to compile Qt 5.7.1 in my machine today.
                      I have downloaded qt-everywhere 5.7.1 and tried to compile in windows 7 platform.
                      I did
                      configure -prefix C:\Qt\5.7.1 -static -release -platform win32-g++ -no-compile-examples -opengl desktop
                      and then
                      mingw32-make
                      After several hours(Slow machine), I met the following error message
                      In file included from release\statemachine.cpp:7:0:
                      release\statemachine.h:10:30: fatal error: QScxmlStateMachine: No such file or directory
                      #include <QScxmlStateMachine>
                      ^
                      compilation terminated.
                      Makefile.Release:135: recipe for target '.obj/release/statemachine.o' failed.

                      Do I have to add some extra options to configure to solve this problem?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Hi,

                        Since you are upgrading, why not 5.9.1 ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Hi,

                          Since you are upgrading, why not 5.9.1 ?

                          S Offline
                          S Offline
                          samdol
                          wrote on last edited by
                          #13

                          @SGaist
                          Because my license is expired and I could not download that version with my account. I used Mingw 4.9.1 to compile it. Do I have to use 5.3 version to compile Qt 5.7.1?

                          jsulmJ 1 Reply Last reply
                          0
                          • S samdol

                            @SGaist
                            Because my license is expired and I could not download that version with my account. I used Mingw 4.9.1 to compile it. Do I have to use 5.3 version to compile Qt 5.7.1?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @samdol Do you have a commercial Qt license?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            S 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @samdol Do you have a commercial Qt license?

                              S Offline
                              S Offline
                              samdol
                              wrote on last edited by
                              #15

                              @jsulm
                              It is paid for developement with Qt. before it is expired, I could download Qt 5.9 alpha version. I am not sure whether I could use this as commercial version because when I download alpha version, my license was still valid.

                              mrjjM 1 Reply Last reply
                              0
                              • S samdol

                                @jsulm
                                It is paid for developement with Qt. before it is expired, I could download Qt 5.9 alpha version. I am not sure whether I could use this as commercial version because when I download alpha version, my license was still valid.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @samdol

                                Hi
                                Can't you just grap it directly then ?
                                https://download.qt.io/official_releases/qt/5.9/5.9.1/

                                Or something Im missing?

                                S 2 Replies Last reply
                                1
                                • mrjjM mrjj

                                  @samdol

                                  Hi
                                  Can't you just grap it directly then ?
                                  https://download.qt.io/official_releases/qt/5.9/5.9.1/

                                  Or something Im missing?

                                  S Offline
                                  S Offline
                                  samdol
                                  wrote on last edited by
                                  #17

                                  @mrjj
                                  I could download enterprise version before 5.9 but since my license expired, I could not gain it. By the way, I could compile Qt 5.6.2 with that configure options but Qt 5.7.1 always spews error when I tried to compile with the same options. I could not understand the method to compile for Qt 5.6.2 did not apply to Qt 5.7.1.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • S samdol

                                    @mrjj
                                    I could download enterprise version before 5.9 but since my license expired, I could not gain it. By the way, I could compile Qt 5.6.2 with that configure options but Qt 5.7.1 always spews error when I tried to compile with the same options. I could not understand the method to compile for Qt 5.6.2 did not apply to Qt 5.7.1.

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @samdol Is there a reason why you compile Qt by yourself?

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    S 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @samdol Is there a reason why you compile Qt by yourself?

                                      S Offline
                                      S Offline
                                      samdol
                                      wrote on last edited by
                                      #19

                                      @jsulm
                                      I need to develop a stand alone application which requires static build of Qt. I could do with Qt 5.6.2 but I could not do it anymore for 5.7.1. I don't know whether there is big change between them.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • S samdol

                                        @jsulm
                                        I need to develop a stand alone application which requires static build of Qt. I could do with Qt 5.6.2 but I could not do it anymore for 5.7.1. I don't know whether there is big change between them.

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        @samdol Why don't you build Qt 5.9.1? This is the latest version. If you say you have errors while compiling you should say which errors and which compiler you're using.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        S 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @samdol Why don't you build Qt 5.9.1? This is the latest version. If you say you have errors while compiling you should say which errors and which compiler you're using.

                                          S Offline
                                          S Offline
                                          samdol
                                          wrote on last edited by
                                          #21

                                          @jsulm
                                          I could not use Qt 5.9.1 as a commercial version because my license has expired.
                                          I downloaded Qt 5.7.1 and configured by
                                          configure -prefix C:\Qt\5.7.1 -static -release -platform win32-g++ -no-compile-examples -opengl desktop
                                          and then
                                          mingw32-make
                                          After several hours(Slow machine), I met the following error message
                                          In file included from release\statemachine.cpp:7:0:
                                          release\statemachine.h:10:30: fatal error: QScxmlStateMachine: No such file or directory
                                          #include <QScxmlStateMachine>
                                          ^
                                          compilation terminated.
                                          Makefile.Release:135: recipe for target '.obj/release/statemachine.o' failed.

                                          This is quite strange, because when I compile Qt 5.6.2 with the same options, It could compile well. I used mingw4.9.3 and mingw7.1 but the results are the same.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved