views:

109

answers:

2

Hi all,

I have my 2nd assignment for C++ class which includes Markov chains, The assignment is simple but I'm not able to figure out what is the best implementation when reading chars from files I have a file around 300k, one of the rules for the assignment is to use Map and Vector classes in Map (key is only string) and values will be the Vectors. When im reading from file, I need to start collecting key pairs Example:

File1.txt
1234567890
1234567890

If Select Markov k=3 I should have in my Map:

key     vector
123  -> 4
456  -> 7
789  -> 0
0/n1 -> 2
234  -> 5
567  -> 8
890  -> /n
/n   -> NULL

Professor suggestion is to read char by char so my algorithm is the following

while (readchar != EOF){
tempstring += readchar
increment index
if index == Markovlevel {   
       get nextchar if =!EOF
       insert nextchar value in vector
       insert tempstring to Map and assign vector         
       unget char
}

}

I omit other details, my main question is that if I have 318,000 characters I will be doing the conditional every time which slows down my computer a lot (brand new MAC pro) sample program from professor executes this file in around 5 seconds Not able to figure out whats the best method to read fixed lenght words from a text file in c++.

Thanks!

+2  A: 

Have you actually timed the program? 318,000 conditionals should be a piece of cake for your brand new MAC pro. That should take only microseconds.

Premature optimization is the root of all evil. Make your program work first, optimization comes second.

amarillion
Thanks! will follow ur advisesoftware Dev phase :)
+2  A: 

Repeated file reading will slow down the program.

Read the file in blocks, of say size 1024, put into a buffer. Then process this buffer as you require for the assignment. Repeat for the next block till you are done with the file.

Amit Kumar