测量ex的数量

2024-10-04 03:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想测量每秒excl调用的次数,但是我的脚本在第一次迭代后终止,因为excl内置了一个leave函数!你知道吗

我想知道如何回到我的剧本或找到一种方法来计数:

t_end = time.time() + 60
counter = 0
while time.time() < t_end:
    def execlNum():
        os.execl('script.py' 'script.py', '0')

    execlNum()
    counter = counter+1
print counter/60

Tags: 方法函数py脚本timecounterscript次数
2条回答

因为exec将当前可执行映像替换为指定的映像,所以不能在循环中执行—可以说execl实际上永远不会返回(当它成功时),因为当前代码不再存在。你知道吗

如果要测量每秒可以执行的execl的数量,可以执行以下操作:

#!/usr/bin/env python2
import time
import sys
import subprocess
import os
# duration of our benchmark
duration = 10
# time limit
limit = time.time() + duration
count = 0
# if we are in the "execl-ed process", take the arguments from the command line
if len(sys.argv)==3:
    limit = float(sys.argv[1])
    # increment the counter
    count = int(sys.argv[2])+1
# if we have time, do another execl (passing the incremented counter)
if time.time()<limit:
    os.execl('execpersec.py', 'execpersec.py', str(limit), str(count))

# if we get here, it means that the 10 seconds have expired; print
# how many exec we managed to do
print count/float(duration), "exec/sec"

但请记住,这比基准测试实际的exec时间(操作系统所需的时间)更像是Python启动时间(以及编译此脚本所需的时间)的基准测试;在我的机器上,此脚本输出58.8 exec/秒,而其直接的C翻译(见下文)产生1484.2 exec/秒。你知道吗

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

int duration = 10;

double mytime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + tv.tv_usec*1E-6;
}

int main(int argc, char *argv[]) {
    const double duration = 10;
    double limit = mytime() + duration;
    long count = 0;
    if(argc==3) {
        limit = atof(argv[1]);
        count = atol(argv[2])+1;
    }
    if(mytime()<limit) {
        char buf_limit[256], buf_count[256];
        sprintf(buf_limit, "%f", limit);
        sprintf(buf_count, "%ld", count);
        execl("./execpersec.x", "./execpersec.x", buf_limit, buf_count, NULL);
    }
    printf("%f exec/sec\n", count/duration);
    return 0;
}

你对^{}调用的假设是完全错误的。此系统调用没有任何“leave function build in”的命名方式,但它有效地用从第一个参数指向此函数的文件中加载的新可执行代码替换当前进程。你知道吗

所以,简单的回答是你不能。你知道吗

你需要用新的方式重新定义你的问题。你知道吗

相关问题 更多 >