Thursday, 14 September 2017

ifstream - Should C++ file read be slower than Ruby or C#?

Completely new to C++.


I'm comparing various aspects of C++, C# and Ruby to see if there's need for mirroring a library. Currently, simple read of a file (post update).


Compiling C++ and C# in VS 2017. C++ is in release(x64) mode (or at least compile then run)


The libraries more or less read a file and split the lines into three which make up the members of an object which are then stored in an array member.


For stress testing I tried a large file 380MB(7M lines) (after update) now getting similar performance with C++ and Ruby,


Purely reading the file and doing nothing else the performance is as below:


Ruby: 7s
C#: 2.5s
C++: 500+s (stopped running after awhile, something's clearly wrong)
C++(release build x64): 7.5s

The code:


#Ruby
file = File.open "test_file.txt"
while !file.eof
line = file.readline
end
//C#
StreamReader file = new StreamReader("test_file.txt");
file.Open();
while((line = file.ReadLine()) != null){
}
//C++
#include "stdafx.h"
#include "string"
#include "iostream"
#include "ctime"
#include "fstream"
int main()
{
std::ios::sync_with_stdio(false);
std::ifstream file;
file.open("c:/sandboxCPP/test_file.txt");
std::string line;
std::clock_t start;
double duration;
start = std::clock();
while (std::getline(file, line)) {
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
std::cout << "\nDuration: " << duration;
while (true)
{
}
return 0;
}

Edit: The following performed incredibly well. 0.03s


vector lines;
string tempString = str.str();
boost::split(lines, tempString, boost::is_any_of("\n"));
start = clock();
cout << "\nCount: " << lines.size();
int count = lines.size();
string s;
for (int i = 0; i < count; i++) {
s = lines[i];
}

s = on the likelihood that I don't know what boost is doing. Changed performance.


Tested with a cout of a random record at the end of the loop.


Thanks

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...