-
I am working on a project involving huge amount of searching in JSON keys. {
"basic":{
"a":[...],
"b":[...],
"c":[...],
"d":[...],
"e":[...],
...
}
} Basically I need to search from A to Z until there is a match, and the search need to be performed for many times (around 10 Million times). For now I just use iterations mentioned in the docs: for (auto& el : Map_Obj["basic"].items())
{
if(el.key() == letter){
//Do something
}
} When the iteration needs to be perform for over 10M times, the speed is dramatically slow, completing in 10~20 minutes or so. However, when I rewrite the code in Node.js and run it on V8, the speed boosts to less than 30 seconds, while the JSON is the same and I am using the same iteration method. for (let key in Map_Obj["basic"]) {
if (Map_Obj["basic"].hasOwnProperty(key)) {
if (key == letter) {
//Do something
}
}
} I've checked my code for multiple times and found nothing, so I am here to enquiry if there is anyway to improve the performance on C++. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Do you compile in Release mode? |
Beta Was this translation helpful? Give feedback.
-
You are doing a linear search on a map, which is about the most inefficient thing you can do.
|
Beta Was this translation helpful? Give feedback.
-
I have to say it's weird. I used the same iteration logic in JS and C++, but the speed is so different. |
Beta Was this translation helpful? Give feedback.
-
Alright, I mapped the JSON mannually in C++, using map<string,vector<string>> = {{"a",{"x","y","z"}}, ...} it turns out that even if I use such a hard-coded map, the performance is totally not improved. So, obviously, the problem has nothing to do with JSON lib, and I am closing the quesion. Such a weird problem, I literally have no idea what caused it. |
Beta Was this translation helpful? Give feedback.
Alright, I mapped the JSON mannually in C++, using
it turns out that even if I use such a hard-coded map, the performance is totally not improved.
So, obviously, the problem has nothing to do with JSON lib, and I am closing the quesion.
Such a weird problem, I literally have no idea what caused it.