Performance comparisons between objects and pointers for C + + performance and comparison of Java and C + + performance

Source: Internet
Author: User

For a more intuitive comparison, OK, let's choose to initialize the object and add to the list as an example.

First, define the following object:

#include <string>#pragmaOnceusing namespacestd;classfirstcppcls{Private:    stringserviceId; stringSystemID; stringsubsystemID; stringappversion; stringCompanyID; stringclustername;  Public: Firstcppcls (void); ~FIRSTCPPCLS (void); InlinestringGetserviceid () {returnServiceId;} InlinestringGetsystemid () {returnSystemID;} InlinestringGetsubsystemid () {returnsubsystemID;} InlinestringGetappversion () {returnappversion;} InlinestringGetcompanyid () {returnCompanyID;} InlinestringGetclustername () {returnclustername;} InlinevoidSetserviceid (stringV) {serviceId =v;} InlinevoidSetsystemid (stringV) {SystemID =v;} InlinevoidSetsubsystemid (stringV) {subsystemID =v;} InlinevoidSetappversion (stringV) {appversion =v;} InlinevoidSetcompanyid (stringV) {CompanyID =v;} InlinevoidSetclustername (stringV) {clustername =v;}}

Test code:

    //Object creation Time comparisonDWORD begin =GetTickCount (); intF; Vector<FirstCPPCls*>VO;  for(i=0;i<100000; i++) {firstcppcls clz; Clz.setappversion ("12.32.33"); Clz.setclustername ("Osm-service"); Clz.setcompanyid ("239383"); Clz.setserviceid ("sysl.1.223"); Clz.setsubsystemid (" at"); Clz.setsystemid (" +"); Vo.push_back (&CLZ); } cout<< vo.size () <<Endl; DWORD End=GetTickCount (); //Print Time Differencecout << (end-begin) << Endl;//average 4800 seconds or soSystem"Pause");

Java:

     Public Static voidMain (string[] args) {List<RouteItem> Routeitems =NewArraylist<routeitem>();        System.out.println (System.currenttimemillis ());  for(inti=0;i<100000;i++) {Routeitem CLZ=NewRouteitem (); Clz.setappversion ("12.32.33"); Clz.setclustername ("Osm-service"); Clz.setcompanyid ("239383"); Clz.setserviceid ("sysl.1.223"); Clz.setsubsystemid ("23"); Clz.setsystemid ("32");        Routeitems.add (CLZ);        } System.out.println (Routeitems.size ()); System.out.println (System.currenttimemillis ());
Average 15ms or so}

Well, the CPP is replaced with a char* pointer:

#include <string>#pragmaOnceusing namespacestd;classfirstcppcls{Private:    Char*serviceId; Char*SystemID; Char*subsystemID; Char*appversion; Char*CompanyID; Char*clustername;  Public: Firstcppcls (void); ~FIRSTCPPCLS (void); InlineChar* Getserviceid () {returnServiceId;} InlineChar* Getsystemid () {returnSystemID;} InlineChar* Getsubsystemid () {returnsubsystemID;} InlineChar* Getappversion () {returnappversion;} InlineChar* Getcompanyid () {returnCompanyID;} InlineChar* Getclustername () {returnclustername;} InlinevoidSetserviceid (Char* v) {serviceId =v;} InlinevoidSetsystemid (Char* v) {SystemID =v;} InlinevoidSetsubsystemid (Char* v) {subsystemID =v;} InlinevoidSetappversion (Char* v) {appversion =v;} InlinevoidSetcompanyid (Char* v) {CompanyID =v;} InlinevoidSetclustername (Char* v) {clustername =v;} };

The test, on average, is about 46ms. It's still slower than Java.

Look at a lot of code that will be used in the char[], as follows:

#include <string>#pragmaOnceusing namespacestd;classfirstcppcls{Private:    Charserviceid[ A]; Charsystemid[4]; Charsubsystemid[4]; Charappversion[ A]; Charcompanyid[6]; Charclustername[ A];  Public: Firstcppcls (void); ~FIRSTCPPCLS (void); InlineChar* Getserviceid () {returnServiceId;} InlineChar* Getsystemid () {returnSystemID;} InlineChar* Getsubsystemid () {returnsubsystemID;} InlineChar* Getappversion () {returnappversion;} InlineChar* Getcompanyid () {returnCompanyID;} InlineChar* Getclustername () {returnclustername;} InlinevoidSetserviceid (Char*v) {memset (serviceId,0, A);    memcpy (Serviceid,v,strlen (v)); } InlinevoidSetsystemid (Char*v) {memset (SystemID,0,4);    memcpy (Systemid,v,strlen (v)); } InlinevoidSetsubsystemid (Char*v) {memset (subsystemID,0,4);    memcpy (Subsystemid,v,strlen (v)); } InlinevoidSetappversion (Char*v) {memset (appversion,0, A);    memcpy (Appversion,v,strlen (v)); } InlinevoidSetcompanyid (Char*v) {memset (CompanyID,0,6);    memcpy (Companyid,v,strlen (v)); } InlinevoidSetclustername (Char*v) {memset (clustername,0, A);    memcpy (Clustername,v,strlen (v)); }};

The test, on average, is about 62ms. or slower than Java, in most cases, in the case of readability, you should use a third.

After testing the vector, map, found that the main CPP is the default is the value of the copy (in short, you can think of Java in the implementation of the clone, of course, the details of the difference is very large, still think so) reason. With pointers, content replication eliminates a lot.

Back in Java, it should be that after JDK 6, although development uses object, the JVM internally converts all objects of the non-primitive type to pointer operations, and CPP has to retain the traditional mechanism for compatibility with earlier reasons.

Performance comparisons between objects and pointers for C + + performance and comparison of Java and C + + performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.