I was curious about how much faster it is to call Eq::eq
to compare two large vectors, versus serializing both vectors to a string and comparing the output strings.
I've done a basic speed-test here: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=73fc1cc61873fa70566eb7b2bf0793f9
The results, in release-mode, for a vector of 100 thousand simple structs:
- Comparing by
Eq::eq(x, y)
: 20ms - Comparing by
serde_json::to_value(x) == serde_json::to_value(y)
: 172ms - Comparing by
serde_json::to_string(x) == serde_json::to_string(y)
: 34ms
These results surprise me a bit; I did not expect that serializing all the way to a string would be nearly as fast as the derived implementation of Eq::eq
.
Is my speed-test flawed in some way? If not, what would explain why there is not a speed advantage to the Eq::eq(x, y)
approach, given that it seems like it should have a lot less work to do?
CodePudding user response:
It is vec.clone()
The results after passing references:
Times. 1:1 2:127 3:39