Тест циклов AWK на локальном VM - PullRequest
0 голосов
/ 10 января 2020

Я пытаюсь понять разницу в 5 секунд между 1000 и 10000 циклами, извлеченными из статьи gawk. Буду признателен за вашу помощь.

#!/usr/bin/gawk -f
# how long does it take to do a few loops?
BEGIN {
    LOOPS=100;
# do the test twice
    start=systime();
    for (i=0;i<LOOPS;i++) {
    }
    end = systime();
# calculate how long it takes to do a dummy test
    do_nothing = end-start;
# now do the test again with the *IMPORTANT* code inside
    start=systime();
    for (i=0;i<LOOPS;i++) {
# How long does this take?
        while ("date" | getline) {
            date = $0;
        }
        close("date");
    }
    end = systime();
    newtime = (end - start) - do_nothing;

    if (newtime <= 0) {
        printf("%d loops were not enough to test, increase it\n", 
            LOOPS);
        exit;
    } else {
        printf("%d loops took %6.4f seconds to execute\n", 
            LOOPS, newtime);
        printf("That's %10.8f seconds per loop\n", 
            (newtime)/LOOPS);
# since the clock has an accuracy of +/- one second, what is the error
        printf("accuracy of this measurement = %6.2f%%\n",
            (1/(newtime))*100);
    }
    exit;
}
[root@krislasnetwork ~]# ./testSpeed.sh
1000 loops took 2.0000 seconds to execute
That's 0.00200000 seconds per loop
accuracy of this measurement =  50.00%
[root@krislasnetwork ~]# vi testSpeed.sh # changing loops from 1000 to 10000
[root@krislasnetwork ~]# ./testSpeed.sh
10000 loops took 15.0000 seconds to execute
That's 0.00150000 seconds per loop
accuracy of this measurement =   6.67%

Похоже, что каждый l oop в течение 10 000 циклов выполняется быстрее, чем каждый l oop в течение 1000. Можете ли вы помочь мне понять это поведение?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...