|
发表于 2009-5-23 23:01:09
|
显示全部楼层
写了个简单的shell脚本来把你的txt转换回二进制数据文件,发现运行超慢,200k的txt就要6s,又用c来写了个程序,发现时间消耗不是一个数量级的...
w10.txt是10份原来的wwww.txt连接成,测试下时间消耗
文本已经由dos格式转成unix格式,去掉了CR- $ ls -n w10.txt
- -rw-r--r-- 1 1000 1000 1927660 05-23 19:05 w10.txt
- $ time cpath/txt2data w10.txt 10.dat
- real 0m0.175s
- user 0m0.148s
- sys 0m0.024s
- $ time shellpath/txt2data w10.txt 10.dat2
- real 1m1.204s
- user 0m43.419s
- sys 0m17.089s
- $ diff 10.dat 10.dat2 #没有输出,文件一致
复制代码 [color="Red"]shell脚本要61s,c程序只要0.175s,汗死...
不知道shell用什么方法可以处理更快
shell脚本- #!/bin/bash
- [ $# -eq 2 ] || exec echo 'Usage: txt2data input.txt output.dat'
- if [ -f "$1" ]; then
- txt_list=$(cat $1)
- else
- exec echo File "$1" not exist!
- fi
- data=
- for hex_txt in $txt_list; do
- printf "\x$hex_txt" >> "$2"
- done
复制代码
c程序- #include <stdlib.h>
- #include <stdio.h>
- main(int argc,char *argv[])
- {
- int asc[2],data,i;
- FILE *ascstream,*outstream;
- if ( argc != 3 )
- {
- printf("Usage: txt2data input.txt ouput.dat\n");
- exit(1);
- }
- if((ascstream=fopen(argv[1],"rt"))==NULL)
- /*只读文本模式打开输入文件*/
- {
- printf("Can not open input file: %s\n",argv[1]);
- exit(2);
- }
- if((outstream=fopen(argv[2],"wb"))==NULL)
- /*只写模式创建二进制输出文件,重名则覆盖已有文件*/
- {
- printf("Can not create file: %s\n",argv[2]);
- fclose(ascstream);
- exit(2);
- }
- asc[0]=fgetc(ascstream);
- while(asc[0] != EOF )
- {
- asc[1]=fgetc(ascstream);
- for(i=0;i<=1;i++)
- {
- if (asc[i] >= 65)
- asc[i]=asc[i]-55;
- else
- asc[i]=asc[i]-48;
- }
- data=asc[0]*16 + asc[1];
- // fwrite(&data,sizeof(char),1,outstream);
- fputc(data,outstream);
- do
- asc[0]=fgetc(ascstream);
- /*忽略空格回车换行*/
- while(asc[0]==0x20 || asc[0]==0x0a || asc[0]==0x0d);
- }
- while(fclose(outstream));
- fclose(ascstream);
- exit(0);
- }
复制代码 |
|