Hi,
Does anyone know how to use Standard C++ to efficiently convert a string to an int or float? This code below uses a template to convert an STL string to type int - but when processing a 50k line CSV file with OHLC (plus date, time, volume, etc). data, it takes a huge amount of time to process.. it is by far the biggest bottleneck - at first I thought it was the stream methods for loading/buffering the ASCII CSV file from the drive, or perhaps pushing back onto a nested vector, but it turns out those are rather fast, and this conversion is quite slow in comparison.
I might try resorting to char[]'s and methods such as atoi() or atof() - or sscanf(), or strtod() - and compare the performance - but would much prefer to keep it Standard, modern and type safe (as the above template does).
Ideally the conversion from ASCII to numerical types, pushed into an STL container would be a one time process; read the data and subsequently store the information in an additional file in some type of binary format (this is called serialization?). I am not sure of where to start with this process - I would like to store standard numerical types such as ints and floats into a binary file, and be able to later reload them in the fastest manner possible. As such, using a template (or any method for that matter) to convert a string into an int will be a big performance hit. I would like the program to read int (or whatever numerical data type is used) directly from a binary file - no conversion - and fast.. Perhaps someone knows of an online resource for such techniques, or can point in the right direction? Maybe there is a great book that discusses modern techniques for this? This way, the performance of the initial conversion from ASCII to numerical types becomes a non-issue (although it is still nice to understand/implement the 'right' or 'best' way to solve a problem).
Thanks.
Does anyone know how to use Standard C++ to efficiently convert a string to an int or float? This code below uses a template to convert an STL string to type int - but when processing a 50k line CSV file with OHLC (plus date, time, volume, etc). data, it takes a huge amount of time to process.. it is by far the biggest bottleneck - at first I thought it was the stream methods for loading/buffering the ASCII CSV file from the drive, or perhaps pushing back onto a nested vector, but it turns out those are rather fast, and this conversion is quite slow in comparison.
Code:
#include ...
template < class T>
bool from_string(T& t, const string& s, ios_base& (*f)(std::ios_base&))
{
istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main(...)
{
string field;
int iconvert(0);
...
from_string< int>(iconvert, field, std::dec);
...
}
Ideally the conversion from ASCII to numerical types, pushed into an STL container would be a one time process; read the data and subsequently store the information in an additional file in some type of binary format (this is called serialization?). I am not sure of where to start with this process - I would like to store standard numerical types such as ints and floats into a binary file, and be able to later reload them in the fastest manner possible. As such, using a template (or any method for that matter) to convert a string into an int will be a big performance hit. I would like the program to read int (or whatever numerical data type is used) directly from a binary file - no conversion - and fast.. Perhaps someone knows of an online resource for such techniques, or can point in the right direction? Maybe there is a great book that discusses modern techniques for this? This way, the performance of the initial conversion from ASCII to numerical types becomes a non-issue (although it is still nice to understand/implement the 'right' or 'best' way to solve a problem).
Thanks.
