i really dont get it:
i am reading points, each holds 3 float values, out of a binary file.
Saving this points in an unordered_map
therefore i try to create a key out of these 3 float values:
first intention:
just use the exact bits as key:
unordered_map<string, vector<float>> points;
string vecToKey( float* a ) {
char bytes[12];
memcpy(bytes, a, 12);
return string(bytes);
}
the point is that i definitely want to eleminate same points this way
but
in an example project reading about 21374 points
the map result size = 10640 points
using following method as key creation results in the proper result
of 10687 points
string vec3ToKey( float a[3] ) {
float a1[3];
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[2];
stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << a1;
return ss.str();
}
the problem is the speed. second method needs about 16 seconds and first method just 1-2 seconds…
i just cant explane myself why there even is a difference …
i appreciate every idea 🙂
The string constructor you’re using stops at the first null byte. Floating point values can contain null bytes. So the string is probably not accurately representing the three floats. You can see by sticking an assert in there:
Another problem is that
bytesmight not contain a null byte, and the program may copy random garbage into the string or otherwise exhibit undefined behavior.I would recommend that you just not try to abuse string this way. You want a key that’s three floats, so use a key that represents exactly that:
std::array<float,3>. Or better yet use a ‘Point’ class since that’s what the three floats represent.Since there’s no built in hash function for arrays you can use something like this: