1. 程式人生 > >C語言生成2000w行資料的兩個實現

C語言生成2000w行資料的兩個實現

以下兩個程式碼均在linux下GCC測試通過

要求:

字串長度為16
字串只能包含大小寫字母和數字
要求生成20000000行的TXT文字


/**********************************
* Author: Ervin_Zhao(2012.6)
* Compile: gcc
* Create a file named test.txt, included 2000w line random string
* string have 16 num used A-Z, a-z, 0-9
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    char array[]="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
    char buff[170000];
    char *buff2 = buff;
    int file = open("test.txt", O_WRONLY|O_CREAT, S_IRWXU|S_IRGRP|S_IROTH);
    int i;
    int temp[4];
    char *p = (char *)temp;
    for(i = 0; i<20000000; i++)
    {
        temp[0] = rand();
        temp[1] = rand();
        temp[2] = rand();
        temp[3] = rand();

        buff2[0] = array[p[0]%(sizeof(array)-1)];
        buff2[1] = array[p[1]%(sizeof(array)-1)];
        buff2[2] = array[p[2]%(sizeof(array)-1)];
        buff2[3] = array[p[3]%(sizeof(array)-1)];

        buff2[4] = array[p[4]%(sizeof(array)-1)];
        buff2[5] = array[p[5]%(sizeof(array)-1)];
        buff2[6] = array[p[6]%(sizeof(array)-1)];
        buff2[7] = array[p[7]%(sizeof(array)-1)];

        buff2[8] = array[p[8]%(sizeof(array)-1)];
        buff2[9] = array[p[9]%(sizeof(array)-1)];
        buff2[10] = array[p[10]%(sizeof(array)-1)];
        buff2[11] = array[p[11]%(sizeof(array)-1)];

        buff2[12] = array[p[12]%(sizeof(array)-1)];
        buff2[13] = array[p[13]%(sizeof(array)-1)];
        buff2[14] = array[p[14]%(sizeof(array)-1)];
        buff2[15] = array[p[15]%(sizeof(array)-1)];

        buff2[16] = '\n';
        buff2 += 17;

        if(buff2 >= (buff+sizeof(buff)))
        {
            write(file, buff, sizeof(buff));
            buff2 = buff;
        }
    }
    close(file);
    return 0;
}

程式執行時間:

real	0m11.494s
user	0m7.860s
sys	0m0.296s



第二個:

/**********************************
* Author: Ervin_Zhao(2012.6)
* Compile: gcc
* Create a file named test.txt, included 2000w line random string
* string have 16 num used A-Z, a-z, 0-9
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <time.h>

int main ( void )
{
    int i, j;
    const int cols = 16;
    const int rows = 20000000;
    const int size = ( cols + 1 ) * rows;
    const char str[] = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
    const char filename[] = "file.txt";
    char * p = NULL;
    char * pStart = NULL;
    int fd;
    p = pStart = (char *)malloc( size * sizeof(char) );
    if ( p == NULL )
    {
        fprintf ( stderr, "記憶體申請失敗。\n" );
        return EXIT_FAILURE;
    }
    srand( time(NULL) );
    for ( i = 0 ; i < rows; i ++ )
    {
        for ( j = 0 ; j < cols; j ++ )
        {
            *(p ++) = str[rand()%(62)];
        }
        *(p ++) = '\n';
    }
    fd = open( filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR );
    if ( fd == -1 )
    {
        fprintf ( stderr, "檔案開啟錯誤。\n" );
        return EXIT_FAILURE;
    }
    write ( fd, pStart, size );
    free( pStart );
    close(fd);
    return EXIT_SUCCESS;
}

程式執行時間:
real    0m4.381s
user    0m3.108s
sys    0m0.336s

生成檔案: