当前位置:网站首页>Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
2022-07-03 16:09:00 【Zafo】
problem :
I made a test to compare string operations in several languages for choosing a language for the server-side application. I did a test to compare string operations in several languages , To choose a language for the server-side application .The results seemed normal until I finally tried C++, which surprised me a lot. The result seems normal , Until I finally tried C++, This surprised me very much .So I wonder if I had missed any optimization and come here for help. So I want to know if I missed any optimizations and come here for help .
The test are mainly intensive string operations, including concatenate and searching. The tests are mainly intensive string operations , Including connection and search .The test is performed on Ubuntu 11.10 amd64, with GCC's version 4.6.1. Test in Ubuntu 11.10 amd64 On the implementation ,GCC Version is 4.6.1.The machine is Dell Optiplex 960, with 4G RAM, and Quad-core CPU. The machine is Dell Optiplex 960,4G Memory , Tetranuclear CPU.
in Python (2.7.2): stay Python (2.7.2) in :
def test(): x = "" limit = 102 * 1024 while len(x) < limit: x += "X" if x.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) > 0: print("Oh my god, this is impossible!") print("x's length is : %d" % len(x))test()
which gives result: This gives the result :
x's length is : 104448real 0m8.799suser 0m8.769ssys 0m0.008s
in Java (OpenJDK-7): stay Java (OpenJDK-7) in :
public class test { public static void main(String[] args) { int x = 0; int limit = 102 * 1024; String s=""; for (; s.length() < limit;) { s += "X"; if (s.indexOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ") > 0) System.out.printf("Find!\n"); } System.out.printf("x's length = %d\n", s.length()); }}
which gives result: This gives the result :
x's length = 104448real 0m50.436suser 0m50.431ssys 0m0.488s
in Javascript (Nodejs 0.6.3) stay Javascript (Nodejs 0.6.3)
function test(){ var x = ""; var limit = 102 * 1024; while (x.length < limit) { x += "X"; if (x.indexOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) > 0) console.log("OK"); } console.log("x's length = " + x.length);}();
which gives result: This gives the result :
x's length = 104448real 0m3.115suser 0m3.084ssys 0m0.048s
in C++ (g++ -Ofast) stay C++ (g++ -Ofast)
It's not surprising that Nodejs performas better than Python or Java.Nodejs Better performance than Python or Java It's not surprising .But I expected libstdc++ would give much better performance than Nodejs, whose result really suprised me. But I expect libstdc++ Will provide more than Nodejs Better performance , Its result really surprised me .
#include <iostream>#include <string>using namespace std;void test(){ int x = 0; int limit = 102 * 1024; string s(""); for (; s.size() < limit;) { s += "X"; if (s.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0) != string::npos) cout << "Find!" << endl; } cout << "x's length = " << s.size() << endl;}int main(){ test();}
which gives result: This gives the result :
x length = 104448real 0m5.905suser 0m5.900ssys 0m0.000s
Brief Summary Brief summary
OK, now let's see the summary: well , Now let's look at the summary :
- javascript on Nodejs(V8): 3.1sNodejs(V8) Upper javascript:3.1s
- Python on CPython 2.7.2 : 8.8sCPython 2.7.2 Upper Python:8.8s
- C++ with libstdc++: 5.9s with libstdc++ Of C++:5.9 second
- Java on OpenJDK 7: 50.4sOpenJDK 7 Upper Java:50.4 second
Surprisingly! It's amazing !I tried "-O2, -O3" in C++ but noting helped. I am here C++ I tried “-O2,-O3”, But it helps to notice .C++ seems about only 50% performance of javascript in V8, and even poor than CPython.C++ stay V8 There seems to be only javascript Of 50% performance , Even better than CPython Still bad .Could anyone explain to me if I had missed some optimization in GCC or is this just the case? Can someone explain to me if I missed GCC Some optimizations in , Or just this situation ?Thank you a lot. Thank you very much .
Solution :
Reference resources : https://stackoom.com/en/question/Yrot边栏推荐
- Hibernate的缓存机制/会话级缓存机制
- Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
- Effect of ARP package on FTP dump under vxworks-6.6 system
- Driver and application communication
- Reflection on some things
- Record a jar package conflict resolution process
- Colab works with Google cloud disk
- App移动端测试【4】apk的操纵
- 用通达信炒股开户安全吗?
猜你喜欢
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
嵌入式开发:避免开源软件的7个理由
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
uploads-labs靶场(附源码分析)(更新中)
[list to map] collectors Tomap syntax sharing (case practice)
TCP拥塞控制详解 | 3. 设计空间
MB10M-ASEMI整流桥MB10M
Microservices - load balancing ribbon
How idea starts run dashboard
随机推荐
nifi从入门到实战(保姆级教程)——flow
Reflection on some things
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
Embedded development: seven reasons to avoid open source software
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
Project -- high concurrency memory pool
[web security] - [SQL injection] - error detection injection
Find mapping relationship
Brush questions -- sword finger offer
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
Effect of ARP package on FTP dump under vxworks-6.6 system
疫情常态化大背景下,关于远程办公的思考|社区征文
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
Under VC, Unicode and ANSI are converted to each other, cstringw and std:: string are converted to each other
Microservice - Nacos registration center and configuration center
Intelij idea efficient skills (III)
How idea starts run dashboard
First!! Is lancet hungry? Official documents
Initial test of scikit learn Library