谁来跟我挑战效率?从文件d.txt中逐个读出能读的浮点数,比如"1.0,3.5,2.2 ..."
缘起这个帖子
http://topic.csdn.net/u/20110218/12/E039E736-CA2F-4168-B06A-14D386D511C5.html
//谁来跟我挑战效率?从文件d.txt中逐个读出能读的浮点数,比如"1.0,3.5,2.2 ..."
#include <stdio.h>
int n,r;
double d;
FILE *f;
void main() {
f=fopen("d.txt","r");
n=0;
while (1) {
r=fscanf(f,"%lf",&d);
if (1==r) {
n++;
printf("[%d]==%lg\n",n,d);//可以试试注释掉这句以后的速度
} else if (0==r) {
fscanf(f,"%*c");
} else break;
}
fclose(f);
}
还是这样吧, 好像基本上是读文件的时间, 扫描16M, 运行100次在我这的时间是14秒左右...
#include <stdio.h>
#include <stdlib.h>
/**
%option caseless
DIGIT[[:digit:]]+
FLT{DIGIT}
[解决办法]
\.{DIGIT}
[解决办法]
{DIGIT}\.
[解决办法]
{DIGIT}\.{DIGIT}
EFLT{FLT}e[\+\-]?{DIGIT}
%%
[+-]?({FLT}
[解决办法]
{EFLT})
[^[:digit:]\.\+\-]+
%%
flex -CeF
**/
struct yy_trans_info
{
int yy_verify;
int yy_nxt;
};
int slove( unsigned char *input , unsigned char *end , double result[] , int size )
{
static const int yy_ec[] = { 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 1, 2, 3, 1, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 5, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1
} ;
static const struct yy_trans_info yy_transition[] = {
{ 0, 0 }, { 0, 80 }, { 0, 0 }, { 0, 78 }, { 1, 14 },
{ 2, 17 }, { 3, 23 }, { 4, 25 }, { 5, 14 }, { 0, 0 },
{ 0, 71 }, { 1, 7 }, { 2, 10 }, { 3, 16 }, { 4, 18 },
{ 5, 7 }, { 0, 2 }, { 0, 64 }, { 1, 18 }, { 0, 3 },
{ 0, 61 }, { 0, 0 }, { 5, 18 }, { 3, 18 }, { 4, 24 },
{ 0, 3 }, { 0, 55 }, { 0, 1 }, { 0, 53 }, { 0, 0 },
{ 4, 20 }, { 3, 25 }, { 4, 16 }, { 5, 32 }, { 0, 2 },
{ 0, 46 }, { 1, 0 }, { 0, 0 }, { 0, 43 }, { 0, 0 },
{ 5, 0 }, { 0, 0 }, { 4, 8 }, { 0, 1 }, { 0, 37 },
{ 0, 1 }, { 0, 35 }, { 3, 9 }, { 4, 0 }, { 5, 16 },
{ 4, 0 }, { 5, 14 }, { 0, 1 }, { 0, 28 }, { 0, 0 },
{ 0, 0 }, { 0, 0 }, { 4, 13 }, { 5, 7 }, { 0, 0 },
{ 0, 21 }, { 0, 0 }, { 2, 8 }, { 0, 0 }, { 4, 14 },
{ 0, 1 }, { 0, 15 }, { 0, 0 }, { 0, 13 }, { 0, 0 },
{ 4, 0 }, { 5, -6 }, { 4, 6 }, { 0, 1 }, { 0, 7 },
{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 4, 0 }, { 0, 0 },
{ 6, 4 }, { 1, 0 }, };
int count , yyc , action;
const struct yy_trans_info *current , *backup, *transinfo;
unsigned char *text , *text_backup;
if( size <= 0 )
return 0;
count = 0;
while( input < end )
{
current = &yy_transition[3];
text = input;
for( yyc = yy_ec[*input] ;(transinfo = ¤t[yyc])->yy_verify == yyc ;yyc = yy_ec[*++input])
{
current += transinfo->yy_nxt;
if( current[-1].yy_nxt )
{
backup = current;
text_backup = input;
}
}
action = current[-1].yy_nxt;
if( 0 == action )
{
current = backup;
input = text_backup + 1;
}
if( 1 == action )
{
result[count++] = strtod( text , NULL );
if( count >= size )
break;
}
}
return count;
}
#ifndef FILE_MAXIMUM_SIZE
#define FILE_MAXIMUM_SIZE(16*1024*1024)
#endif
#ifndef RESULT_MAXIMUM_SIZE
#define RESULT_MAXIMUM_SIZE(8*1024*1024)
#endif
int main( int argc , char* argv[] )
{
int i , j , n;
unsigned char *input = malloc( FILE_MAXIMUM_SIZE + 4 );
double *result = malloc( sizeof( double ) * RESULT_MAXIMUM_SIZE );
for( i = 1; i < argc; ++i )
{
FILE *fp = fopen( argv[i] , "rb" );
if( NULL == fp )
{
fprintf( stderr , "[ERROR]open %s failed.\n" , argv[i] );
}
else
{
size_t len = fread( input , 1 , FILE_MAXIMUM_SIZE , fp );
input[len] = 0xEE;input[len+1] = input[len+2] = 0;
fclose( fp );
for( j = 0; j < 100; ++j )
n = slove( input , input + len, result , RESULT_MAXIMUM_SIZE );
printf( "process %s (len = %d) return %d\n" , argv[i] , (int)len , n );
#if 0
for( j = 0; j < n; ++j )
{
printf( "result [ %d ] : %lg\n" , j , result[j] );
}
#endif
}
}
return 0;
}