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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File writing performance comparing

File writing performance comparing

Scheduled Pinned Locked Moved C / C++ / MFC
iosperformancequestion
2 Posts 2 Posters 0 Views 1 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.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    Tested by the sample code below, the result: 1. write is much slower than the other two 2. fwrite is twice as fast as ofstream.write I know write is not buffered, so it is much slower, I think the performance of the other two should be close, but why not?

    #define WIN32_LEAN_AND_MEAN

    #include <stdio.h>
    #include <tchar.h>
    #include <io.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <assert.h>
    #include <fstream>
    #include <windows.h>

    #pragma warning(disable : 4996)

    int _tmain(int argc, _TCHAR* argv[])
    {
    const int buf_size = 1<<20;
    char * buf = new char[buf_size];
    FILE * f1 = fopen("d:/test1", "w");
    assert(f1);
    int f2 = open("d:/test2", _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IWRITE);
    assert(f2!=-1);

    DWORD t;
    
    t = GetTickCount();	
    for (size\_t i=0; i<buf\_size; ++i)
    	fwrite(buf+i, 1, 1, f1);
    fclose(f1);
    printf("count of ticks used: %d bytes\\n", GetTickCount()-t);
    
    t = GetTickCount();
    for (int i=0; i<buf\_size; ++i)
    	write(f2, buf+i, 1);
    close(f2);
    printf("count of ticks used: %d\\n", GetTickCount()-t);
    
    t = GetTickCount();
    std::ofstream ofs("d:/test3", std::ios\_base::out | std::ios\_base::binary | std::ios\_base::trunc);
    for (int i=0; i<buf\_size; ++i)
    	ofs.write(buf+i, 1);
    ofs.close();
    printf("count of ticks used: %d", GetTickCount()-t);
    
    delete buf;
    
    getchar();
    return 0;
    

    }

    enhzflepE 1 Reply Last reply
    0
    • F followait

      Tested by the sample code below, the result: 1. write is much slower than the other two 2. fwrite is twice as fast as ofstream.write I know write is not buffered, so it is much slower, I think the performance of the other two should be close, but why not?

      #define WIN32_LEAN_AND_MEAN

      #include <stdio.h>
      #include <tchar.h>
      #include <io.h>
      #include <fcntl.h>
      #include <sys/stat.h>
      #include <assert.h>
      #include <fstream>
      #include <windows.h>

      #pragma warning(disable : 4996)

      int _tmain(int argc, _TCHAR* argv[])
      {
      const int buf_size = 1<<20;
      char * buf = new char[buf_size];
      FILE * f1 = fopen("d:/test1", "w");
      assert(f1);
      int f2 = open("d:/test2", _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IWRITE);
      assert(f2!=-1);

      DWORD t;
      
      t = GetTickCount();	
      for (size\_t i=0; i<buf\_size; ++i)
      	fwrite(buf+i, 1, 1, f1);
      fclose(f1);
      printf("count of ticks used: %d bytes\\n", GetTickCount()-t);
      
      t = GetTickCount();
      for (int i=0; i<buf\_size; ++i)
      	write(f2, buf+i, 1);
      close(f2);
      printf("count of ticks used: %d\\n", GetTickCount()-t);
      
      t = GetTickCount();
      std::ofstream ofs("d:/test3", std::ios\_base::out | std::ios\_base::binary | std::ios\_base::trunc);
      for (int i=0; i<buf\_size; ++i)
      	ofs.write(buf+i, 1);
      ofs.close();
      printf("count of ticks used: %d", GetTickCount()-t);
      
      delete buf;
      
      getchar();
      return 0;
      

      }

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      I don't know enough about fstream - though it looks as though it's performance may vary between implementations. Just ran your code through MinGW 3.4.5, and I see a very different picture to the one you describe. Over here, your code timings are: fstream: 78 fwrite: 109 write: 3391 I know iostream & fstream increase program size considerably, (read several 100k) and would have expected fstream to be second, in terms of performance, though this clearly appears not to be the case. :confused:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

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