性能测试之字符串比较; C、C++和JAVA

郁闷,等了几天没人理我。现在把在各个平台测试的结果贴出来。 

 

OS

Compiler

Results(Seconds)

 

C

C++

Java

Case 1

Windows

Visual Studio

4

3

8

Case 2

Windows

GNU C/C++

3

8

8

Case 3

Linux

GNU C/C++

3

13

6

Case 4

Solaris 10

Sun CC

5.6

17

14

Case 5

AIX 5.3

IBM xlC

6.5

26

22.5

Case 6

HPUX 11.11

HP aCC

8.5

20

16

C++怎么会这么差呢??? 

下面是测试代码:

C代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void compTest(){
 time_t t1,t2;
 char *s1 = NULL;
 char *s2 = NULL;
 int i, j;
 int eCnt = 0;
 int nCnt = 0;

 s1 = (char *)malloc(100);
 memset(s1, 0, 100);
 sprintf(s1, "%s", "abcdefghijklmnopqrstuvwxyz");

 s2 = (char *)malloc(100);
 memset(s2, 0, 100);
 sprintf(s2, "%s", "abcdefghijklmnopqrstuvwxy1");

 time(&t1);
 for(j=0; j<10; j++)
  for(i=0; i<10*1024*1024; i++){
   if(strcmp(s1, s2) != 0){
    nCnt++;
   }
   else{
    eCnt++;
   }
  }

 time(&t2);
 printf("eCnt[%d] nCnt[%d] %ld seconds eclapsed!/n", eCnt, nCnt, t2-t1);
}

int main()
{
 compTest();
 return 0;
}

C++代码:

#include <iostream>
using namespace std;

void compTest(){
 time_t t1,t2;
 std::string s1="abcdefghijklmnopqrstuvwxyz";
 std::string s2="abcdefghijklmnopqrstuvwxy1";
 int eCnt = 0;
 int nCnt = 0;

 time(&t1);
 for(int j=0; j<10; j++)
  for(int i=0; i<10*1024*1024; i++){
   //if(s1.compare(s2) != 0){
   if(s1 != s2){
    nCnt++;
   }
   else{
    eCnt++;
   }
 }

 time(&t2);
 cout <<"eCnt[" <<eCnt <<"] nCnt[" <<nCnt <<"]/t";
 cout << t2-t1 <<"seconds eclapsed!/n";
}

int main()
{
 compTest();
 return 0;
}

Java 代码:

import java.util.*;

public class tt5{
 void compTest(){
  long t1, t2;
  String s1="abcdefghijklmnopqrstuvwxyz";
  String s2="abcdefghijklmnopqrstuvwxy1";
  int eCnt = 0;
  int nCnt = 0;

  t1 = Calendar.getInstance().getTimeInMillis()/1000;
  for(int i=0; i<10; i++){
   for(int j=0; j<10*1024*1024; j++){
    if(s1.compareTo(s2) != 0){
     nCnt++;
    }
    else{
     eCnt++;
    }
   }
  }
  t2 = Calendar.getInstance().getTimeInMillis()/1000;

  System.out.println("eCnt[" + eCnt + "] nCnt[" + nCnt + "], ");
  System.out.println("Comparason over and " + (t2-t1) + "seconds eclapsed!");
 }

 public static void main(String[] args){
  tt5 mtt = new tt5();
  mtt.compTest();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值