从C程序中运行C程序
这个问题很好地解释了我有三个不同的C程序,为了尝试比较它们的效率,我尝试通过让它们运行几次来测试它们的运行时间,改变它们的参数(与所用的时间成正比),并写下每个程序为某个参数运行所需的时间(以便稍后我可以绘制结果) 下面是我的代码从C程序中运行C程序,c,runtime,system,sh,C,Runtime,System,Sh,这个问题很好地解释了我有三个不同的C程序,为了尝试比较它们的效率,我尝试通过让它们运行几次来测试它们的运行时间,改变它们的参数(与所用的时间成正比),并写下每个程序为某个参数运行所需的时间(以便稍后我可以绘制结果) 下面是我的代码 # include <stdio.h> # include <stdlib.h> # include <math.h> # include <time.h> int main(
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
int main(void){
int i;
struct timeval bni, bmi, bfi, bnf, bmf, bff;
FILE *in;
char filename1[30] = "shuff.dat";
int a1,a2,b1,b2,c1,c2;
char command[100];
in = fopen(filename1, "w");
//for(i = 0; i<=100000; i +=100){
for(i = 0; i<=10; i +=1){
if (snprintf(command, sizeof(command), "./barajas_n.x %d", i) < sizeof(command)){
a1 = gettimeofday(&bni , NULL);
system(command);
a2 = gettimeofday(&bnf , NULL);
}
if (snprintf(command, sizeof(command), "./barajas_m.x %d", i) < sizeof(command)){
b1 = gettimeofday(&bmi , NULL);
system(command);
b2 = gettimeofday(&bmf , NULL);
}
if (snprintf(command, sizeof(command), "./barajas_fy.x %d", i) < sizeof(command)){
c1 = gettimeofday(&bfi , NULL);
system(command);
c2 = gettimeofday(&bff , NULL);
}
fprintf(in, "%d %d %d %d \n", i, (a2-a1),(b2-b1),(c2-c1));
}
fclose(in);
}
snprintf()尝试system()
函数只接受一个const char*
类型的参数
,就像这样
./barajas_*.x i
char command[100];
if (snprintf(command, sizeof(command), "barajas_n.x %d", i) < sizeof(command))
{
a1 = gettimeofday(&bni , NULL);
system(command);
a2 = gettimeofday(&bfi , NULL);
}
然后打印经过的时间
printf("Elapsed time: %f\n", elapsed_time(&bni, &bfi);
正如前面提到的另一个答案,您需要添加斜杠来执行程序,/programname
而不是.programname
,但这是另一个解决方案:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
/* This function, just calculates the difference in seconds, between end and start */
float elapsed_time(struct timeval *end, struct timeval *start)
{
struct timeval result;
if (end->tv_usec - start->tv_usec > 1.0E6)
{
float adjust;
adjust = (end->tv_usec - start->tv_usec) / 1.0E6;
start->tv_usec += 1.0E6 * adjust;
start->tv_sec -= adjust;
}
result.tv_sec = end->tv_sec - start->tv_sec;
result.tv_usec = end->tv_usec - start->tv_usec;
return result.tv_sec + result.tv_usec / 1.0E6;
}
/* this function will execute the command and wrap the system call
* with 'gettimeofday()' so you can return the elapsed time while
* the called program was running.
*
* It also builds the command string with the right parameter.
*/
float run_command_and_return_time(const char *const program, int parameter)
{
char command[100];
struct timeval start;
struct timeval end;
int result;
/* check that sprintf didn't need more characters */
result = snprintf(command, sizeof(command), "%s %d", program, parameter);
if ((result >= sizeof(command)) || (result < 0))
return -1.0;
gettimeofday(&start, NULL);
system(command);
gettimeofday(&end, NULL);
return elapsed_time(&end, &start);
}
int
main(int argc, char **argv)
{
char cwd[PATH_MAX];
const char *filename;
FILE *output;
filename = "shuff.dat";
output = fopen(filename, "w");
if (output == NULL)
return -1;
/* get the current working directory */
getcwd(cwd, sizeof(cwd));
/* add the cwd to the PATH variable, so your barajas_*.x programs are found,
* this way you don't need the ./bara... anymore, just bara... will do it.
*/
setenv("PATH", cwd, 1);
/* from here it's pretty evident what the program does */
for (int i = 0 ; i < 10 ; ++i)
{
float a, b, c;
a = run_command_and_return_time("barajas_n.x", i);
b = run_command_and_return_time("barajas_m.x", i);
c = run_command_and_return_time("barajas_fy.x", i);
fprintf(output, "%d %f %f %f \n", i, a, b, c);
}
/* don't forget to close the output file */
fclose(output);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
/*此函数仅计算结束和开始之间的秒差*/
浮点运行时间(结构时间值*结束,结构时间值*开始)
{
结构时间值结果;
如果(结束->电视节目-开始->电视节目>1.0E6)
{
浮动调整;
调整=(结束->电视节目-开始->电视节目)/1.0E6;
开始->tv_usec+=1.0E6*调整;
开始->电视节目-=调整;
}
result.tv_sec=end->tv_sec-start->tv_sec;
result.tv_usec=end->tv_usec-start->tv_usec;
返回result.tv_sec+result.tv_usec/1.0E6;
}
/*此函数将执行命令并包装系统调用
*使用“gettimeofday()”可以在
*被调用的程序正在运行。
*
*它还使用正确的参数生成命令字符串。
*/
浮点运行命令和返回时间(常量字符*常量程序,int参数)
{
char命令[100];
结构timeval启动;
结构timeval-end;
int结果;
/*检查sprintf是否不需要更多字符*/
结果=snprintf(命令,sizeof(命令),%s%d”,程序,参数);
如果((结果>=sizeof(命令))| |(结果<0))
回报率-1.0;
gettimeofday(&start,NULL);
系统(指挥部);
gettimeofday(&end,NULL);
返回经过的时间(&end,&start);
}
int
主(内部argc,字符**argv)
{
char cwd[PATH_MAX];
常量字符*文件名;
文件*输出;
filename=“shuff.dat”;
输出=fopen(文件名,“w”);
if(输出==NULL)
返回-1;
/*获取当前工作目录*/
getcwd(cwd,sizeof(cwd));
/*将cwd添加到PATH变量中,这样就可以找到您的barajas*.x程序,
*这样你就不需要了./bara…再也不需要了,只要bara…就可以了。
*/
setenv(“路径”,cwd,1);
/*从这里可以很明显地看出这个程序的作用*/
对于(int i=0;i<10;++i)
{
浮子a、b、c;
a=运行命令和返回时间(“barajas\u n.x”,i);
b=运行命令和返回时间(“barajas\u m.x”,i);
c=运行命令和返回时间(“barajas\u fy.x”,i);
fprintf(输出,“%d%f%f%f\n”,i、a、b、c);
}
/*不要忘记关闭输出文件*/
fclose(输出);
返回0;
}
如果需要生成命令,请尝试使用snprintf()
,如下所示
./barajas_*.x i
char command[100];
if (snprintf(command, sizeof(command), "barajas_n.x %d", i) < sizeof(command))
{
a1 = gettimeofday(&bni , NULL);
system(command);
a2 = gettimeofday(&bfi , NULL);
}
然后打印经过的时间
printf("Elapsed time: %f\n", elapsed_time(&bni, &bfi);
正如前面提到的另一个答案,您需要添加斜杠来执行程序,/programname
而不是.programname
,但这是另一个解决方案:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
/* This function, just calculates the difference in seconds, between end and start */
float elapsed_time(struct timeval *end, struct timeval *start)
{
struct timeval result;
if (end->tv_usec - start->tv_usec > 1.0E6)
{
float adjust;
adjust = (end->tv_usec - start->tv_usec) / 1.0E6;
start->tv_usec += 1.0E6 * adjust;
start->tv_sec -= adjust;
}
result.tv_sec = end->tv_sec - start->tv_sec;
result.tv_usec = end->tv_usec - start->tv_usec;
return result.tv_sec + result.tv_usec / 1.0E6;
}
/* this function will execute the command and wrap the system call
* with 'gettimeofday()' so you can return the elapsed time while
* the called program was running.
*
* It also builds the command string with the right parameter.
*/
float run_command_and_return_time(const char *const program, int parameter)
{
char command[100];
struct timeval start;
struct timeval end;
int result;
/* check that sprintf didn't need more characters */
result = snprintf(command, sizeof(command), "%s %d", program, parameter);
if ((result >= sizeof(command)) || (result < 0))
return -1.0;
gettimeofday(&start, NULL);
system(command);
gettimeofday(&end, NULL);
return elapsed_time(&end, &start);
}
int
main(int argc, char **argv)
{
char cwd[PATH_MAX];
const char *filename;
FILE *output;
filename = "shuff.dat";
output = fopen(filename, "w");
if (output == NULL)
return -1;
/* get the current working directory */
getcwd(cwd, sizeof(cwd));
/* add the cwd to the PATH variable, so your barajas_*.x programs are found,
* this way you don't need the ./bara... anymore, just bara... will do it.
*/
setenv("PATH", cwd, 1);
/* from here it's pretty evident what the program does */
for (int i = 0 ; i < 10 ; ++i)
{
float a, b, c;
a = run_command_and_return_time("barajas_n.x", i);
b = run_command_and_return_time("barajas_m.x", i);
c = run_command_and_return_time("barajas_fy.x", i);
fprintf(output, "%d %f %f %f \n", i, a, b, c);
}
/* don't forget to close the output file */
fclose(output);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
/*此函数仅计算结束和开始之间的秒差*/
浮点运行时间(结构时间值*结束,结构时间值*开始)
{
结构时间值结果;
如果(结束->电视节目-开始->电视节目>1.0E6)
{
浮动调整;
调整=(结束->电视节目-开始->电视节目)/1.0E6;
开始->tv_usec+=1.0E6*调整;
开始->电视节目-=调整;
}
result.tv_sec=end->tv_sec-start->tv_sec;
result.tv_usec=end->tv_usec-start->tv_usec;
返回result.tv_sec+result.tv_usec/1.0E6;
}
/*此函数将执行命令并包装系统调用
*使用“gettimeofday()”可以在
*被调用的程序正在运行。
*
*它还使用正确的参数生成命令字符串。
*/
浮点运行命令和返回时间(常量字符*常量程序,int参数)
{
char命令[100];
结构timeval启动;
结构timeval-end;
int结果;
/*检查sprintf是否不需要更多字符*/
结果=snprintf(命令,sizeof(命令),%s%d”,程序,参数);
如果((结果>=sizeof(命令))| |(结果<0))
回报率-1.0;
gettimeofday(&start,NULL);
系统(指挥部);
gettimeofday(&end,NULL);
返回经过的时间(&end,&start);
}
int
主(内部argc,字符**argv)
{
char cwd[PATH_MAX];
常量字符*文件名;
文件*输出;
filename=“shuff.dat”;
输出=fopen(文件名,“w”);
if(输出==NULL)
返回-1;
/*获取当前工作目录*/
getcwd(cwd,sizeof(cwd));
/*将cwd添加到PATH变量中,这样就可以找到您的barajas*.x程序,
*这样你就不需要了./bara…再也不需要了,只要bara…就可以了。
*/
环境变量