awk, c, java 的IO速度测试

首先用c++得到的一个简单但庞大的测试文件:1000万行的“A B C D”

createFile.cpp 生成的测试文件叫test.in

1#include<cstdio>
2#define LINENUM 10000000
3int main(){
4    freopen("test.in","w",stdout);
5    for(int i=0;i<LINENUM;++i){
6        printf("A\tB\tC\tD\n");
7    }
8    return 0;
9}
awk脚本read.awk
01#!/usr/bin/awk -f
02BEGIN{
03    lines = 0
04    start=systime()
05}
06{
07    str = $3
08    lines ++
09}
10END{
11    print "line count = " lines
12    end=systime()
13    print "run time = " end-start "s"
14}
c代码 read.c
01#include<stdio.h>
02#include<string.h>
03#include<time.h>
04FILE * fp;
05int main(int argc,char * argv[]){
06    int i;
07    for(i=0;i<argc;++i){
08        printf("%s\n",argv[i]);
09    }
10    if(argc > 1){
11        fp = fopen(argv[1],"r");
12        if(fp == NULL){
13            printf("can't open file\n");
14            return 0;
15        }
16        char str[20];
17        char str2[20];
18        long lines = 0;
19        long start = clock();
20        while(fgets(str,16,fp)!=NULL){
21            ++lines;
22            strcpy(str2,str);
23        }
24        long end = clock();
25        printf("lines count = %ld\n",lines);
26        printf("time cost = %lf\n",1.0*(end-start)/CLOCKS_PER_SEC);
27        fclose(fp);
28    }
29    return 0;
30}
java代码 read.java
01import java.io.*;
02import java.util.*;
03public class read{
04    public static void main(String args[]){
05        for(int i=0;i<args.length;++i)   System.out.println(args[i]);
06        if(args.length > 0){
07            Scanner in;
08            try{
09                in = new Scanner(new FileInputStream(new File(args[0])));
10            }catch(Exception e){
11                System.out.println("exception occurs");
12                in = new Scanner(System.in);
13            }
14            String s,s2;
15            int lines = 0;
16            long start = System.currentTimeMillis();
17            while(in.hasNext()){
18                s = in.nextLine();
19                ++ lines;
20                s2 = s;
21            }
22            long end = System.currentTimeMillis();
23            System.out.println("lines count = "+lines);
24            System.out.println("time cost = "+1.0*(end-start)/1000+"s");
25        }
26    }
27}
测试脚本 testScript.sh
01#!/bin/sh
02echo "test file is creating"
03g++ createFile.cpp -o createFile
04./createFile
05echo "test file created\n\nnow test awk script"
06./read.awk test.in
07echo "awk script test finish\n\nnow test c programme language"
08gcc read.c -o read
09./read test.in
10echo "c test finish\n\nnow test Java"
11javac read.java
12java read test.in
13echo "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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值