0

1 秒あたりの execl 呼び出しの数を測定したいのですが、execl には 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
4

2 に答える 2

2

現在の実行可能イメージを指定したものに置き換えるためexec、ループで実行することはできませんexecl。現在のコードが存在しなくなるため、(成功した場合) 実際には決して返らないと言えます。

execl1 秒間に実行できる回数を測定したい場合は、次のようにします。

#!/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時間 (OS の必要に応じて) をベンチマークするというよりも、Python の起動時間 (およびこのスクリプトをコンパイルするのに必要な時間) のベンチマークに似ていることに注意してください。私のマシンでは、このスクリプトは 58.8 exec/sec を出力しますが、単純な C 変換 (以下を参照) は 1484.2 exec/sec を生成します。

#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;
}
于 2015-11-25T22:25:19.277 に答える