Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enhance the speed of my C++ program in reading delimited text files?
    primarykey
    data
    text
    <p>I show you C# and C++ code that execute the same job: to read the same text file delimited by “|” and save with “#” delimited text.</p> <p>When I execute C++ program, the time elapsed is 169 seconds.</p> <p>UPDATE 1: Thanks to Seth (compilation with: cl /EHsc /Ox /Ob2 /Oi) and GWW for changing the positions of string s outside the loops, the elapsed time was reduced to 53 seconds. I updated the code also.</p> <p>UPDATE 2: Do you have any other suggestion to enhace the C++ code?</p> <p>When I execute the C# program, the elapsed time is 34 seconds!</p> <p>The question is, how can I enhance the speed of C++ comparing with the C# one?</p> <p>C++ Program:</p> <pre><code>int main () { Timer t; cout &lt;&lt; t.ShowStart() &lt;&lt; endl; ifstream input("in.txt"); ofstream output("out.txt", ios::out); char const row_delim = '\n'; char const field_delim = '|'; string s1, s2; while (input) { if (!getline( input, s1, row_delim )) break; istringstream iss(s1); while (iss) { if (!getline(iss, s2, field_delim )) break; output &lt;&lt; s2 &lt;&lt; "#"; } output &lt;&lt; "\n"; } t.Stop(); cout &lt;&lt; t.ShowEnd() &lt;&lt; endl; cout &lt;&lt; "Executed in: " &lt;&lt; t.ElapsedSeconds() &lt;&lt; " seconds." &lt;&lt; endl; return 0; } </code></pre> <p>C# program:</p> <pre><code> static void Main(string[] args) { long i; Stopwatch sw = new Stopwatch(); Console.WriteLine(DateTime.Now); sw.Start(); StreamReader sr = new StreamReader("in.txt", Encoding.Default); StreamWriter wr = new StreamWriter("out.txt", false, Encoding.Default); object[] cols = new object[0]; // allocates more elements automatically when filling string line; while (!string.Equals(line = sr.ReadLine(), null)) // Fastest way { cols = line.Split('|'); // Faster than using a List&lt;&gt; foreach (object col in cols) wr.Write(col + "#"); wr.WriteLine(); } sw.Stop(); Console.WriteLine("Conteo tomó {0} secs", sw.Elapsed); Console.WriteLine(DateTime.Now); } </code></pre> <h1>UPDATE 3:</h1> <p>Well, I must say I am very happy for the help received and because the answer to my question has been satisfied.</p> <p>I changed the text of the question a little to be more specific, and I tested the solutions that kindly raised Molbdlino and Bo Persson.</p> <p>Keeping Seth indications for the compile command (i.e. cl /EHsc /Ox /Ob2 /Oi pgm.cpp):</p> <p>Bo Persson's solution took 18 seconds on average to complete the execution, really a good one taking in account that the code is near to what I like).</p> <p>Molbdlino solution took 6 seconds on average, really amazing!! (thanks to Constantine also).</p> <p>Never too late to learn, and I learned valuable things with my question.</p> <p>My best regards.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload