zoukankan      html  css  js  c++  java
  • C语言生成2000w行数据

    最近一直抽空学习shell,脚本语言看多了多多少少有些蛋疼不适,所以捡起以前遇到的一个C语言的问题看看。

    原先应该是在C++吧关注的一个帖子,楼主为了测试数据库性能需要如下形式的数据
    要求:

    • 字符串长度为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] = '
    ';
            buff2 += 17;
    
            if(buff2 >= (buff+sizeof(buff)))
            {
                write(file, buff, sizeof(buff));
                buff2 = buff;
            }
        }
        close(file);
        return 0;
    }
    
    

    这个程序结构的清晰,易于理解,真是个不错的程序

    最初收藏的时候没时间去看,最近有空细看了下,在生成随机数那里看了半天,自己的底子实在是有点薄、

    通过实验,原理是这样的。定义为INT形的数组,rand();函数产生8位的十六进制数,四个元素的数组,那么就是32位。

    通过p指针把数组重新“翻译”,作为字符读取,2位一个字符,那么就是16个字符。通过array[p[0]%(sizeof(array)-1)]就可以随机取得array中的字符了

    而且为了避免频繁读写IO,内存中待数据1万行才写入文件中,勉强看懂,叫自己写肯定是写不出。这就是学过和学会C语言的区别吧、

    下面来看看另一个程序

    /**********************************
    * Author:yjf_victor(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, "内存申请失败。
    " );
            return EXIT_FAILURE;
        }
        srand( time(NULL) );
        for ( i = 0 ; i < rows; i ++ )
        {
            for ( j = 0 ; j < cols; j ++ )
            {
                *(p ++) = str[rand()%(62)];
            }
            *(p ++) = '
    ';
        }
        fd = open( filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR );
        if ( fd == -1 )
        {
            fprintf ( stderr, "文件打开错误。
    " );
            return EXIT_FAILURE;
        }
        write ( fd, pStart, size );
        free( pStart );
        close(fd);
        return EXIT_SUCCESS;
    }
    
    

    本以为这个更难一些呢,读起来确是很好理解。而且程序写的很棒,参数易于修改

    有时想想,大一学的C语言,也不深入学习,也不去实践、也只够写个学生信息管理系统了。

    这个生成2000w行数据的,估计我会一个一个的生成随机数,然后估计一顿饭的时间是等不出来结果了…==

    有吧友也通过汇编实现1秒5,由于没有环境,就不运行了,有兴趣的可以去看原帖,原帖地址:http://tieba.baidu.com/p/1626351476 (已为坟,勿回)

    想来shell应该也能实现这一要求,所以去弄了个shell版本的

    tr -dc A-Za-z0-9 < /dev/urandom |fold -w 16 |head -20000000 > file.txt
    
    

    比较简单,一行码~嘿嘿,效率嘛、有舍就有得嘛~~
    初学shell就是伤不起。。。连入门都没,别说写出高效简洁的shell脚本了,本来这个shell想自己写来着,但是下意识的想去用C语言的过程化设计来实现,思维有很多时候跳不出来

    这真是。。。路漫漫,慢慢走吧、、

  • 相关阅读:
    zookeeper 4 letter 描述与实践
    zookeeper理论
    Zookeeper的功能以及工作原理
    zookeeper
    VMware安装、配置CentOS
    python安装requests (win7 & centos7)
    Centos 6.4 32位 gcc 升级(已验证)
    Centos6.4编译安装Node.js(已验证)
    使用supervisor提高nodejs调试效率 (已验证)
    tar.xz文件如何解压 (已验证)
  • 原文地址:https://www.cnblogs.com/ishell/p/4240137.html
Copyright © 2011-2022 走看看