Thread: Disk IO Speed

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    Disk IO Speed

    ok, i got an object, call him bob. im looking for the fastest way to populate a stl container like vector or map of bobs from a file. i was told a while ago i could use the copy function to directly load the entire file into the vector, however im unclear of how to do this, or the rules governing the copy. is this the fastest way? is it faster than looping reading each one individually? copy(ifstream.begin(),ifstream.end(),vec_bob.begin ()); <-would something like that be what im looking for? Thanks for the help.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The most usual way this is accomplished is to use istream_iterator 's .
    Scott meyers wrote an article in his more effective c++ book called " be alert for c++'s most vexing parse" that shows how to do this and the pitfalls of getting it wrong.
    std:: omitted for brevity.
    Code:
    ifstream file("file.dat");
    vector<int> bob( (istream_iterator<int>(file)), istream_iterator<int>());
    This is technically correct.
    this below isn't....
    Code:
    ifstream file("file.dat");
    vector<int> bob(istream_iterator<int>(file),istream_iterator<int>());
    Can you work out why the first should work according to the c++ standard and yet the second will not. To be portable between compilers tho it is often better coded like this...
    Code:
    ifstream file("file.dat");
    istream_iterator<int> filebegin(file);
    istream_iterator<int> fileend;
    vector<int> bob(filebegin,fileend);
    Of course this just illustrates range construction from a file. If the vector is precreated you can use vector::insert() instead.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    thats an interesting way to do it, i havent seen that way before. how is it speed wise? im worried because im potentially loading a very large dictionary of bob's, and execution time is a major consideration for this particular gadget. im just worried about loading 10,000 bobs and taking 5 minutes to read and load them.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    I just tried those and am getting the error:

    error C2664: '__thiscall std::vector<int,class std::allocator<int> >::std::vector<int,class std::allocator<int> >(unsigned int,const int &,const class std::allocator
    <int> &)' : cannot convert parameter 1 from 'class std::istream_iterator<int,char,struct std::char_traits<char> >' to 'unsigned int'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why are you using vector<int> surely you would want vector<bob> or whatever your class is anyway go here and get this because you obviously are gonna be needing it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Stoned_Coder
    anyway go here and get this because you obviously are gonna be needing it.
    There is a God! I had no idea such a thing existed...

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yep and he smokes pot!!!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    int was just a simple example of a datatype. i would like to get it working with a simple one like int before i try a complicated class like my bob. and yes, i have a feeling ill be using that error decoder ALOT. thanks for posting it. you should put it in the FAQ i never took the time to read =P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flight Simulator Wind Speed!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 12:40 AM
  2. Replies: 6
    Last Post: 01-08-2006, 02:49 PM
  3. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM
  4. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM
  5. 30G hd and Speed Disk
    By kevinalm in forum Tech Board
    Replies: 2
    Last Post: 10-27-2002, 04:25 PM