• 本地编译并运行文件,测试运行时间并判断运行结果


    需求是这样的:在正式加ACM试题之前,首先要进行测试,比如代码运行时间测试,输出结果的正确性等,一切都正常才能添加到比赛中。

    这个程序实现的功能也比较简单:

    1.对源代码进行编译

    2.运行编译生成的文件,计算程序的运行时间

    3. 比较输出结果和标准输出是否相同

    /*************************************************************************
        > File Name: 本地测试代码.c
        > Author: ma6174
        > Mail: ma6174@163.com 
        > Created Time: 2012年02月24日 星期五 16时39分46秒
     ***********************************************************************
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/time.h>
    #include<time.h>
    void compile()
    {
        char solution[1000];
        char compiler[1000]="g++ -o temp_sol ";
        printf("请输入源码文件:\n");
        gets(solution);
        int i,len_sol=strlen(solution);
        for(i=0;i<len_sol-1;i++)
        {
            compiler[16+i]=solution[i];
        }
        system(compiler);
    }
    void count_time()
    {
        char run[1000]="./temp_sol < ";
        char input_file[1000];
        printf("请输入input数据文件:\n");
        gets(input_file);
        int i,len_input_file=strlen(input_file);
        for(i=0;i<len_input_file;i++)
        {
            run[13+i]=input_file[i];
        }
        strcat(run," > /tmp/acm_test/output");
        struct timeval tv_start,tv_end;
        gettimeofday(&tv_start,NULL);
        system(run);
        gettimeofday(&tv_end,NULL);
        printf("程序运行时间为:%lf秒\n",tv_end.tv_sec-tv_start.tv_sec+(tv_end.tv_usec-tv_start.tv_usec)/1000000.0);
    }
    int compare()
    {
        printf("请输入标准输出文件:\n");
        char output[1000];
        gets(output);
        char diff[1000]="cmp -s ";
        strcat(diff,"/tmp/acm_test/output ");
        strcat(diff,output);
        int status=system(diff);
        if(fopen("/tmp/acm_test/output","rt")==NULL)
        {
            printf("文件读取错误!\n");
            return -1;
        }
        if(status==0)
        {
            printf("输出完全相同\n");
        }
        else
        {
            printf("输出不相同\n");
        }

    }
    int main()
    {
        compile();
        count_time();
        compare();

    }

    收获:

    这个程序是在linux环境下写的,也是在linux下用的,所以有些功能实现起来和windows不太一样,比如测试时间函数。

    我以前发表过一篇博客讨论关于统计程序运行时间的问题,那是在windows下的,在linux下是这样统计的:

    struct timeval tv_start,tv_end;
    gettimeofday(&tv_start,NULL);
    system(run);
    gettimeofday(&tv_end,NULL);
    printf("程序运行时间为:%lf秒\n",tv_end.tv_sec-tv_start.tv_sec+(tv_end.tv_usec-tv_start.tv_usec)/1000000.0);

    其中timeval的结构体定义是这样的:

    struct timeval {
        time_t      tv_sec;     /* seconds */
        suseconds_t tv_usec;    /* microseconds 微秒=1/10^6秒 */

    }; 

    还有就是system()函数调用系统命令是有返回值的,有时候可以根据返回值判断执行结果!

    博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/

    对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


  • 相关阅读:
    Jenkins使用三:管理slave节点(配置SSH公钥和私钥)
    Jenkins使用二:新建任务
    Jenkins使用一:CentOS7安装Jenkins
    CentOS安装MongoDB
    测开之路七十八:shell之函数和参数
    测开之路七十七:shell之if、case、for、while
    测开之路七十六:linux变量和环境变量
    测开之路七十五:linux常用命令
    linux暴露端口可以被外部访问
    WEB框架概述(译)
  • 原文地址:https://www.cnblogs.com/ma6174/p/2367205.html
Copyright © 2020-2023  润新知