当前位置:网站首页>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边栏推荐
- Chinese translation of Tagore's floating birds (1~10)
- Reflection on some things
- Driver and application communication
- “用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Reading notes of "micro service design" (Part 2)
- 【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
- Colab works with Google cloud disk
- Jmeter线程组功能介绍
- 2022年Q2加密市场投融资报告:GameFi成为投资关键词
猜你喜欢
Remote file contains actual operation
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Microservice - declarative interface call openfeign
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
记一次jar包冲突解决过程
Mixlab编辑团队招募队友啦~~
Embedded development: seven reasons to avoid open source software
Mongodb installation and basic operation
App mobile terminal test [4] APK operation
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
随机推荐
About text selection in web pages and counting the length of selected text
Principles of several common IO models
Getting started with Message Oriented Middleware
Effect of ARP package on FTP dump under vxworks-6.6 system
无心剑中译泰戈尔《漂鸟集(1~10)》
Expression of request header in different countries and languages
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
EditText request focus - EditText request focus
Please be prepared to lose your job at any time within 3 years?
利用MySQL中的乐观锁和悲观锁实现分布式锁
Project -- high concurrency memory pool
First knowledge of database
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
"Everyday Mathematics" serial 56: February 25
Large CSV split and merge
Go语言自学系列 | golang中的if else if语句
Mixlab编辑团队招募队友啦~~
一些事情的反思
Is it safe to open an account with tongdaxin?