【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
首先用c++得到的一个简单但庞大的测试文件:1000万行的“A B C D”
createFile.cpp 生成的测试文件叫test.in
#include<cstdio>
#define LINENUM 10000000
int main(){
freopen("test.in","w",stdout);
for(int i=0;i<LINENUM;++i){
printf("A\tB\tC\tD\n");
}
return 0;
}
awk脚本read.awk
#!/usr/bin/awk -f
BEGIN{
lines = 0
start=systime()
}
{
str = $3
lines ++
}
END{
print "line count = " lines
end=systime()
print "run time = " end-start "s"
}
c代码 read.c
#include<stdio.h>
#include<string.h>
#include<time.h>
FILE * fp;
int main(int argc,char * argv[]){
int i;
for(i=0;i<argc;++i){
printf("%s\n",argv[i]);
}
if(argc > 1){
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("can't open file\n");
return 0;
}
char str[20];
char str2[20];
long lines = 0;
long start = clock();
while(fgets(str,16,fp)!=NULL){
++lines;
strcpy(str2,str);
}
long end = clock();
printf("lines count = %ld\n",lines);
printf("time cost = %lf\n",1.0*(end-start)/CLOCKS_PER_SEC);
fclose(fp);
}
return 0;
}
java代码 read.java
import java.io.*;
import java.util.*;
public class read{
public static void main(String args[]){
for(int i=0;i<args.length;++i) System.out.println(args[i]);
if(args.length > 0){
Scanner in;
try{
in = new Scanner(new FileInputStream(new File(args[0])));
}catch(Exception e){
System.out.println("exception occurs");
in = new Scanner(System.in);
}
String s,s2;
int lines = 0;
long start = System.currentTimeMillis();
while(in.hasNext()){
s = in.nextLine();
++ lines;
s2 = s;
}
long end = System.currentTimeMillis();
System.out.println("lines count = "+lines);
System.out.println("time cost = "+1.0*(end-start)/1000+"s");
}
}
}
测试脚本 testScript.sh
#!/bin/sh
echo "test file is creating"
g++ createFile.cpp -o createFile
./createFile
echo "test file created\n\nnow test awk script"
./read.awk test.in
echo "awk script test finish\n\nnow test c programme language"
gcc read.c -o read
./read test.in
echo "c test finish\n\nnow test Java"
javac read.java
java read test.in
echo "java test finish\n"
测试结果:
test file is creating
test file created
now test awk script
line count = 10000000
run time = 4s
awk script test finish
now test c programme language
./read
test.in
lines count = 10000000
time cost = 0.870000
c test finish
now test Java
test.in
lines count = 10000000
time cost = 16.968s
java test finish
来源:oschina
链接:https://my.oschina.net/u/109107/blog/15383