当前位置:网站首页>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.008sin 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.488sin 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.048sin 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.000sBrief 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边栏推荐
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- Large CSV split and merge
- From the 18th line to the first line, the new story of the network security industry
- Location of software installation information and system services in the registry
- Break through 1million, sword finger 2million!
- 用通达信炒股开户安全吗?
- App移动端测试【3】ADB命令
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- Redis installation under windows and Linux systems
猜你喜欢
![[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)](/img/68/3721975cf33fcfacc28dc4d3d6a5ca.jpg)
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)

关于网页中的文本选择以及统计选中文本长度

Nifi from introduction to practice (nanny level tutorial) - flow

Microservices Seata distributed transactions

【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)

请做好3年内随时失业的准备?

Shell script import and export data

Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package

Embedded development: seven reasons to avoid open source software

First knowledge of database
随机推荐
首发!!lancet饿了么官方文档
《天天数学》连载56:二月二十五日
App mobile terminal test [3] ADB command
Mongodb installation and basic operation
Shell script import and export data
相同切入点的抽取
How to thicken the brush in the graphical interface
Initial test of scikit learn Library
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
Persisting in output requires continuous learning
pyinstaller不是内部或外部命令,也不是可运行的程序 或批处理文件
Subclass hides the function with the same name of the parent class
Expression of request header in different countries and languages
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
Principles of several common IO models
Redis installation under windows and Linux systems
Qt插件之自定义插件构建和使用
From the 18th line to the first line, the new story of the network security industry
Large CSV split and merge