|
-
November 19th, 2002, 10:52 AM
#1
Iterators- Help..
Hi,
I have a problem in hand now.Any help is appreciated.
I have 2 files.I have to read data from 1 file & write to another file.Iam doing that right now as follows:
Opening the "file to read" in a stream.Reading it to a buffer & then writing the buffer to the "output stream".(In Binary mode)
The code is like as follows:
// opening the file to read
ifstream inputFile("File to read",ios::in|ios::binary);
if(inputFile.is_open())
{
istream: os_type beginpos = inputFile.tellg();
inputFile.seekg(0,ios::end);
istream: os_type endpos = inputFile.tellg();
inputFile.seekg(0, ios::beg);
int numBytes = endpos - beginpos;
char *buffer= new char[numBytes+1];
inputFile.read(buffer,numBytes);
//File to write
if(!OutPutFile.eof())
m_ResourceGroup.seekp(0,ios::end);
OutPutFile.write(buffer,numBytes);
}
Now this way of writing is causing performance problems.I would like to use the istream & ostream iterators instead of the "read" & "write" calls.Iam no good in iterators & I havent used it in file operations.
Can any one show me how to do it with respect to my code above...
Thanks...
-
November 19th, 2002, 10:55 AM
#2
Hum, what sort of performance problem do you get ?
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
November 19th, 2002, 11:00 AM
#3
When I check my whole application with a "Performance Analyser",This function takes a lot of percentage time compared to the other function calls.So I want to make this call faster.Lots of file will be read using this Function,Thats why...
-
November 19th, 2002, 11:04 AM
#4
Hum, well, the basic problem is that you are reading and writing to the harddisk, which is inherently slow. I don't think iterators would help speed things up. You might want to eliminate a few of the seek operations and try using c-style fread and fwrite.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
November 19th, 2002, 11:12 AM
#5
Kohinoor, you only seek 3 times. If you are copying large files, then the hard disk is most likely your bottleneck -- as Yves pointed out. If you are copying many, many small files in this manner, then the seeking (which also happens to be related to the hard disk) is probably your bottleneck. I'd be very surprised if your performance had anything to do with C/C++.
- Kevin
-
November 19th, 2002, 11:12 AM
#6
mmm..,Okay.I will remove some of the seek operations as u said.This "iteartor" is an idea from my group leader.So he wants that to be done & tested for performance.He is a kind of adamant person.So I have to do it.
Could u please tell me with respect to my code how,Iterators can be used..
-
November 19th, 2002, 11:33 AM
#7
Do iterators here mean file pointers? Or something more quixotic?
One way to get better performance, if the files are realatively small compared to memory, would be to read the input file into memory and build the output file in memory. Then, iterators for an stl vector or mere pointer array accessing can be used, and the output can be saved when done. As well, if this is on a Windows system, file mapping may be better still since it will conserve physical memory usage (though your virtual space will still be the same) and map in only those pages you access. Similar functionality can be found on other OS's.
I hope I have helped!
[Edit: This only works if this is to be done over and over, which is what I assumed from the performance issue... Sorry I forgot to state on first posting]
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
November 19th, 2002, 11:36 AM
#8
I don't believe std::fstream, std::ifstream, or std: fstream have iterators. Anyway, iterators are nice for traversing complex structures such as linked-lists, trees, etc.... But where a simple while loop or a for loop with simple math will do, iterators have no performance gain (and may possibly have a performance hit).
Personally, I don't see how using iterators (you'd have to create your own here -- yuck) would help.
- Kevin
I hope your boss can understand your dilemma.
-
November 19th, 2002, 11:38 AM
#9
Thanks for ur suggestion.
By iterators,I meant istream_iterator & ostream_iteartor.
-
November 19th, 2002, 11:40 AM
#10
That's supposed to read std:: ofstream (without the space)above. CodeGuru automatically replaces the combination of ':' and 'o' with a yawn...
How does one get around that without inserting a space?
Also, how does have a graphic displayed by their names in the postings?
-
November 19th, 2002, 11:48 AM
#11
Well, you learn something new every day. The book I learned the STL from only discusses istream_iterator and ostream_iterator for one paragraph -- and that paragraph shows up 16 chapters before istream and ostream. Some book!
Thanks for enlightening me.
- Kevin
P.S. I still don't see how the iterators are going to improve performance...
-
November 19th, 2002, 11:48 AM
#12
I just read now that istreambuf_ iterator & ostreambuf_iterator does the job.Iam working on binary data.I dont know wheather that will work or not...
-
November 19th, 2002, 11:56 AM
#13
Originally posted by KevinHall
That's supposed to read std:: ofstream (without the space)above. CodeGuru automatically replaces the combination of ':' and 'o' with a yawn...
How does one get around that without inserting a space?
Also, how does have a graphic displayed by their names in the postings?
When you post, there is a check-box at the bottom
Disable Smilies in This Post
To choose an avatar, go to user cp, Edit Options, Change Avatar.
For Kohinoor, I think that using stream iterators will only make your performance issue worse, if it does change anything. It might also not change a thing.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
November 19th, 2002, 12:13 PM
#14
Just a note: I don't know if you left code
out of your original post or not, but you
do a "new []" and no "delete []".
-
November 19th, 2002, 12:28 PM
#15
I see you kevin....
Wow. Something more quixotic that I never heard of. Great lesson here!
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|