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. ifstream slow to close

ifstream slow to close

Scheduled Pinned Locked Moved C / C++ / MFC
helpiosperformancequestion
1 Posts 1 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.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    Hi, I'm getting some terrible performance when calling ifstream close. It takes about the same amount of time to close() as it takes to read my 64 Mb of data! I've been playing around with it and found it only happens if I have been writing to a file recently. Here's my test code:

    void readTest(const std::string & path)
    {
    ifstream in;
    in.open(path.c_str(), ios_base::in | ios_base::binary);
    char * buffer = new char[67108864];
    in.read(buffer, 67108864);
    SW_ELAPSED_MSG()
    in.close();
    SW_ELAPSED_MSG()
    delete [] buffer;
    }

    void writeTest(const std::string & path)
    {
    ofstream out;
    out.open(path.c_str(), ios_base::out | ios_base::binary);
    char * buffer = new char[67108864];
    out.write(buffer, 67108864);
    //out.flush();
    out.close();
    delete [] buffer;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    writeTest("testfile");
    SW_START();
    readTest("testfile");
    SW_ELAPSED_MSG()
    return 0;
    }

    Note my macros do the timing and display it on screen. I don't believe there's any problem with that code. So anyone know why? I'm closing the streams. Why is the read stream affected at all by the write stream? Also as you can see I have a flush() statement in there. It doesn't help, commented or not.

    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