On Tue, Jun 28, 2022 at 9:56 PM Yan Li <yl.mechan...@gmail.com> wrote: > > Question: > Go 161.385148ms vs C++ ms:31 > Go 851.517707ms vs C++ ms:104 > My question is why traversing go map Significantly slowly? > Can it be improved?
I don't know exactly why there is a speed difference. That said, there are significant differences between Go map and C++ unordered_map. For example: - Go map iteration is in random order. C++ unordered_map iteration is unpredictable but consistent. - If you add an element to a Go map during the iteration, the behavior is specified. If you add an element to a C++ unordered_map all iterators may be invalidated, breaking the loop. Also, there is an implementation difference: the C++ code is compiled inline with the exact types being used. This takes more compile time but produces faster run time. The Go code uses a single shared implementation for maps of all types. This takes less compile time but more run time. Perhaps a combination of all these things explains the difference, or perhaps there is more going on that I don't know about. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVnYnGacowVGgzoUPT6Q-o0SoA_ne%2B0LABJ80Lbs%3DsHjA%40mail.gmail.com.