Re:The ups and downs of porting 50k lines of C++ to Go (2015)
by apta on Thursday March 14, 2019 @05:10PM
(#19393753)
And Java is 2x faster than golang in this benchmark when I ran it. Many benchmarks are to be taken with a grain of salt.
golang: 7.76 real 8.67 user 0.08 sys
Java: 3.74 real 3.58 user 0.46 sys
public class Test {
public static void main(String[] args) {
final int numOfStrings = 100_000;
final int numIters = 1_000;
var numStrings = new String[numOfStrings];
for (int i = 0; i < numStrings.length; i++) {
numStrings[i] = Integer.toString(i);
}
for (int i = 0; i < numIters; i++) {
var strToInt = new java.util.HashMap<String, Integer>(numOfStrings);
for (final var s : numStrings) {
strToInt.put(s, s.length());
}
}
}
}
Re:The ups and downs of porting 50k lines of C++ to Go (2015)
by yoklov on Saturday March 16, 2019 @02:57AM
(#19406695)
What's happening is that the std::unordered_map was a mistake. Many parts of it's API prevent implementing it efficiently (For example, every insertion requires allocation, even if you reserve). It's widely regarded, within and outside the standards committee, as a very unfortunate mistake.
swisstable (absl::flat_hash_map) is a popular choice right now for the hash table to use for C++ code that cares about performance. There are other choices as well (dense_hash_map, F14FastMap, etc), But honestly most choices these days are worse than std::unordered_map (well, boost::unordered_map is probably just as bad, since std's implementation was taken wholesale from boost without any thought!)
upped it to 10M ops and used .reserve to avoid unwanted allocs.
Go 1.12 is 2X faster than C++.
This was totally unexpected as I feel the basic algorithms are simple and there is not much you can do to make it faster.
If you have know what is going on, please share.