您的位置:首页 > 编程语言 > C语言/C++

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

2009-11-27 15:47 399 查看
郁闷,等了几天没人理我。现在把在各个平台测试的结果贴出来。

 

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: