char* to custom type
I have a custom type. Let's say it's a 3D point type with intensity, so it'll look like this :
struct Point{
public:
double x,y,z;
double intensity;
//Imagine a constructor or everything we need for such a class here
};
and another class that use a vector of that point.
class Whatever{
//...
public:
std::vector<Point> myPts;
//...
};
I would like to be able to create this vector from a file. It means I have a file like this:
X1 Y1 Z1 I1
X2 Y2 Z2 I2
X3 Y3 Z3 I3
....
Xn Yn Zn In
And I would like to find a fast technique to separate each lines and build a point per lines, and build my vector. Since it's an operation that I will have to do a lot, I am looking for the fastest way.
The basic solution would be to read the file line by line, convert into stringstream and create from this stringstream :
while(std::getline(file, line)){
std::istringstream iss(line);
Point pt;
iss >> pt.x >> pt.y >> pt.z >> pt.intensity;
vector.add(pt);
}
But this is too much time consuming. So The second method would be to read the file (entire, or a part of the file) in a buffer and format the buffer to create the vector from it. Using a memory mapped file, I can read fastly into a buffer,, but How to format the buffer to create my points without using stringstream which I believe is slow?
EDIT :
I used this code :
std::clock_t begin = clock();
std::clock_t loadIntoFile, loadInVec;
std::ifstream file(filename);
if (file) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
loadIntoFile = clock();
while (!buffer.eof()) {
Point pt = Point();
buffer >> pt.x >> pt.y >> pt.z >> pt.intens >> pt.r >> pt.g >> pt.b;
pts.push_back(pt);
}
loadInVec = clock();
}
std::cout << "file size" << pts.size() << std::endl;
std::cout << "load into file : " << (loadIntoFile - begin) / (double)CLOCKS_PER_SEC << std::endl;
std::cout << "load into vec : " << (loadInVec - loadIntoFile) / (double)CLOCKS_PER_SEC << std::endl;
And the result is :
file size : 7756849
load into file : 2.619
load into vec : 31.532
EDIT2 :
After removing the stringstream buffer, I had a time of 34.604s
And after changing push_back by emplace_back 34.023s
c++ io
|
show 17 more comments
I have a custom type. Let's say it's a 3D point type with intensity, so it'll look like this :
struct Point{
public:
double x,y,z;
double intensity;
//Imagine a constructor or everything we need for such a class here
};
and another class that use a vector of that point.
class Whatever{
//...
public:
std::vector<Point> myPts;
//...
};
I would like to be able to create this vector from a file. It means I have a file like this:
X1 Y1 Z1 I1
X2 Y2 Z2 I2
X3 Y3 Z3 I3
....
Xn Yn Zn In
And I would like to find a fast technique to separate each lines and build a point per lines, and build my vector. Since it's an operation that I will have to do a lot, I am looking for the fastest way.
The basic solution would be to read the file line by line, convert into stringstream and create from this stringstream :
while(std::getline(file, line)){
std::istringstream iss(line);
Point pt;
iss >> pt.x >> pt.y >> pt.z >> pt.intensity;
vector.add(pt);
}
But this is too much time consuming. So The second method would be to read the file (entire, or a part of the file) in a buffer and format the buffer to create the vector from it. Using a memory mapped file, I can read fastly into a buffer,, but How to format the buffer to create my points without using stringstream which I believe is slow?
EDIT :
I used this code :
std::clock_t begin = clock();
std::clock_t loadIntoFile, loadInVec;
std::ifstream file(filename);
if (file) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
loadIntoFile = clock();
while (!buffer.eof()) {
Point pt = Point();
buffer >> pt.x >> pt.y >> pt.z >> pt.intens >> pt.r >> pt.g >> pt.b;
pts.push_back(pt);
}
loadInVec = clock();
}
std::cout << "file size" << pts.size() << std::endl;
std::cout << "load into file : " << (loadIntoFile - begin) / (double)CLOCKS_PER_SEC << std::endl;
std::cout << "load into vec : " << (loadInVec - loadIntoFile) / (double)CLOCKS_PER_SEC << std::endl;
And the result is :
file size : 7756849
load into file : 2.619
load into vec : 31.532
EDIT2 :
After removing the stringstream buffer, I had a time of 34.604s
And after changing push_back by emplace_back 34.023s
c++ io
1
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
1
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
1
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
2
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
1
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38
|
show 17 more comments
I have a custom type. Let's say it's a 3D point type with intensity, so it'll look like this :
struct Point{
public:
double x,y,z;
double intensity;
//Imagine a constructor or everything we need for such a class here
};
and another class that use a vector of that point.
class Whatever{
//...
public:
std::vector<Point> myPts;
//...
};
I would like to be able to create this vector from a file. It means I have a file like this:
X1 Y1 Z1 I1
X2 Y2 Z2 I2
X3 Y3 Z3 I3
....
Xn Yn Zn In
And I would like to find a fast technique to separate each lines and build a point per lines, and build my vector. Since it's an operation that I will have to do a lot, I am looking for the fastest way.
The basic solution would be to read the file line by line, convert into stringstream and create from this stringstream :
while(std::getline(file, line)){
std::istringstream iss(line);
Point pt;
iss >> pt.x >> pt.y >> pt.z >> pt.intensity;
vector.add(pt);
}
But this is too much time consuming. So The second method would be to read the file (entire, or a part of the file) in a buffer and format the buffer to create the vector from it. Using a memory mapped file, I can read fastly into a buffer,, but How to format the buffer to create my points without using stringstream which I believe is slow?
EDIT :
I used this code :
std::clock_t begin = clock();
std::clock_t loadIntoFile, loadInVec;
std::ifstream file(filename);
if (file) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
loadIntoFile = clock();
while (!buffer.eof()) {
Point pt = Point();
buffer >> pt.x >> pt.y >> pt.z >> pt.intens >> pt.r >> pt.g >> pt.b;
pts.push_back(pt);
}
loadInVec = clock();
}
std::cout << "file size" << pts.size() << std::endl;
std::cout << "load into file : " << (loadIntoFile - begin) / (double)CLOCKS_PER_SEC << std::endl;
std::cout << "load into vec : " << (loadInVec - loadIntoFile) / (double)CLOCKS_PER_SEC << std::endl;
And the result is :
file size : 7756849
load into file : 2.619
load into vec : 31.532
EDIT2 :
After removing the stringstream buffer, I had a time of 34.604s
And after changing push_back by emplace_back 34.023s
c++ io
I have a custom type. Let's say it's a 3D point type with intensity, so it'll look like this :
struct Point{
public:
double x,y,z;
double intensity;
//Imagine a constructor or everything we need for such a class here
};
and another class that use a vector of that point.
class Whatever{
//...
public:
std::vector<Point> myPts;
//...
};
I would like to be able to create this vector from a file. It means I have a file like this:
X1 Y1 Z1 I1
X2 Y2 Z2 I2
X3 Y3 Z3 I3
....
Xn Yn Zn In
And I would like to find a fast technique to separate each lines and build a point per lines, and build my vector. Since it's an operation that I will have to do a lot, I am looking for the fastest way.
The basic solution would be to read the file line by line, convert into stringstream and create from this stringstream :
while(std::getline(file, line)){
std::istringstream iss(line);
Point pt;
iss >> pt.x >> pt.y >> pt.z >> pt.intensity;
vector.add(pt);
}
But this is too much time consuming. So The second method would be to read the file (entire, or a part of the file) in a buffer and format the buffer to create the vector from it. Using a memory mapped file, I can read fastly into a buffer,, but How to format the buffer to create my points without using stringstream which I believe is slow?
EDIT :
I used this code :
std::clock_t begin = clock();
std::clock_t loadIntoFile, loadInVec;
std::ifstream file(filename);
if (file) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
loadIntoFile = clock();
while (!buffer.eof()) {
Point pt = Point();
buffer >> pt.x >> pt.y >> pt.z >> pt.intens >> pt.r >> pt.g >> pt.b;
pts.push_back(pt);
}
loadInVec = clock();
}
std::cout << "file size" << pts.size() << std::endl;
std::cout << "load into file : " << (loadIntoFile - begin) / (double)CLOCKS_PER_SEC << std::endl;
std::cout << "load into vec : " << (loadInVec - loadIntoFile) / (double)CLOCKS_PER_SEC << std::endl;
And the result is :
file size : 7756849
load into file : 2.619
load into vec : 31.532
EDIT2 :
After removing the stringstream buffer, I had a time of 34.604s
And after changing push_back by emplace_back 34.023s
c++ io
c++ io
edited Nov 20 '18 at 13:45
Raph Schim
asked Nov 20 '18 at 13:18
Raph SchimRaph Schim
18616
18616
1
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
1
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
1
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
2
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
1
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38
|
show 17 more comments
1
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
1
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
1
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
2
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
1
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38
1
1
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
1
1
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
1
1
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
2
2
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
1
1
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38
|
show 17 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393899%2fchar-to-custom-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393899%2fchar-to-custom-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you measured that it's to "time consuming"? Is this a true bottleneck in your program? It's not a possible case of premature optimizations? And don't "believe" something, measure, benchmark, profile.
– Some programmer dude
Nov 20 '18 at 13:21
1
why are you using the string buffer? Just read directly from the file stream
– user463035818
Nov 20 '18 at 13:38
1
If you're very concerned about performance you shouldn't be storing your data as text.
– molbdnilo
Nov 20 '18 at 13:47
2
imho, no matter how you put it, the correct answer to "How to format the buffer to create my points without using stringstream which I believe is slow?" has to convince you that "which I believe is slow" is going into the wrong direction. Reading data from files is slow, thats not c++ streams fault. Try to reduce the amount of data, use binary files, but blaming stringstream out of a believe doesnt help much ;)
– user463035818
Nov 20 '18 at 13:53
1
You want to create N points. You have a text file with 4*N numbers represented as text. You need 4*N numbers represented as machine doubles. You can minimise all other overhead, but these 4*N text to double conversions, and copying the file to memory, will be there no matter what you do. They will likely remain your limiting factor.
– n.m.
Nov 20 '18 at 14:38