[LINUX] I forgot to evaluate the performance because of the habit of skipping "I tried to aim for the fastest FizzBuzz"

Introduction

I forgot to evaluate the performance in the article I wrote earlier, "I tried to aim for the fastest FizzBuzz". I'm an idiot. For that reason, we evaluated the performance of the missing program.

Evaluation method

The last FizzBuzz program I created

$ time -p ./fizzbuzz >/dev/null
real 0.00
user 0.00
sys 0.00
$ 

Even if you execute such as, it is not useful because the execution time of the program is too short to measure with the time command. With a little ingenuity

 $ time -p (for i in `seq 1 10000`; do ./fizzbuzz; done) >/dev/null
real 6.76
user 4.55
sys 4.41

A method of executing multiple times and dividing the execution time by the number of executions can be considered, but this method is not adopted because the repetition by the shell seems to be more expensive than the execution time of the program. This time, I decided to make up a short program that repeatedly executes the same program and use it.

repeat.c


#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <spawn.h>
#include <sys/wait.h>

int main(int argc, char* argv[])
{
    int opt;
    int n = 1;
    bool f = false;
    int fd = -1;
    opterr = 0;
    while ((opt = getopt(argc, argv, "hfn:")) != -1) {
        switch (opt) {
        case 'h':
        default:
            fprintf(stderr, "Usage: repeat [options]... command\n");
            exit(EXIT_FAILURE);
            break;
        case 'f':
            f = true;
            break;
        case 'n':
            n = atoi(optarg);
            break;
        }
    }
    if (f) {
        fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
    }
    for (int i = 1; i <= n; i++) {
        if (f && fd >= 0) {
            while (write(fd, "1", 1) == -1 && errno == EINTR) {
                ;
            }
        }
        pid_t pid;
        posix_spawn(&pid, argv[optind], NULL, NULL, &argv[optind], NULL);
        int status;
        wait(&status);
        if (status) {
            fprintf(stderr, "pid %d: %d\n", pid, status);
            exit(status);
        }
    }
    if (f && fd >= 0) {
        close(fd);
    }
}

this

$ gcc -Wall -Wextra -O2 repeat.c -o repeat

Compile as and

python


$ time -p ./repeat -n 10000 ./fizzbuzz >/dev/null
real 1.84
user 1.44
sys 0.48
$ 

Use as. The command line option \ -n value'specifies the number of times the program is executed repeatedly, and the number output by the time command is divided by the number of executions to obtain the execution time per execution. Once the program is read from the disc, its contents are registered in the page cache, and it will not go to read the disc during repeated executions. Since we also want to evaluate the load of reading the program from the disk, we have provided a function to execute the program from the state where the page cache is cleared by specifying the command line option \ -f'. The page cache is cleared by writing 1 to / proc / sys / vm / drop_caches, and since administrator privileges are required, it is necessary to use sudo together.

$ sudo time -p ./repeat -n 10000 -f ./fizzbuzz >/dev/null
real 85.90
user 3.26
sys 78.85
$ 

This is also the execution time including the program load time by dividing the output number by the number of repetitions as in the previous example.

Measurement result

No cache clear (* The number is the time (seconds) executed 10,000 times)

C language dynamic link version C language static link version Assembler version
real 4.55 2.99 1.82
user 3.65 2.37 1.43
sys 0.91 0.69 0.47

There is cash clear (* same as above)

C language dynamic link version C language static link version Assembler version
real 121.74 142.39 85.90
user 8.15 9.46 3.26
sys 93.45 95.96 78.85

Executable file size (* unit is bytes)

C language dynamic link version C language static link version Assembler version
6112 714736 808

Evaluation

in conclusion

The end.

Recommended Posts

I forgot to evaluate the performance because of the habit of skipping "I tried to aim for the fastest FizzBuzz"
I tried to correct the keystone of the image
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
I tried to automate the face hiding work of the coordination image for wear
I tried to summarize the basic form of GPLVM
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I tried to summarize the string operations of Python
I tried to find the entropy of the image with python
I tried porting the code written for TensorFlow to Theano
[Horse Racing] I tried to quantify the strength of racehorses
I tried to get the location information of Odakyu Bus
Try to evaluate the performance of machine learning / regression model
I tried to find the average of the sequence with TensorFlow
[Python] I tried to visualize the follow relationship of Twitter
[Machine learning] I tried to summarize the theory of Adaboost
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
I tried to build the SD boot image of LicheePi Nano
I tried to expand the size of the logical volume with LVM
I tried to visualize Boeing of violin performance by pose estimation
I tried to improve the efficiency of daily work with Python
I tried to visualize the common condition of VTuber channel viewers
I tried to move the ball
I tried to estimate the interval.
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to get the batting results of Hachinai using image processing
I tried to visualize the age group and rate distribution of Atcoder
I tried transcribing the news of the example business integration to Amazon Transcribe
zoom I tried to quantify the degree of excitement of the story at the meeting
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried how to improve the accuracy of my own Neural Network
I tried to get the authentication code of Qiita API with Python.
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to summarize the logical way of thinking about object orientation.
I tried to restore because the Oracle database initialization parameter setting failed.
I tried to find the optimal path of the dreamland by (quantum) annealing
I tried to verify and analyze the acceleration of Python by Cython
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]
I tried to visualize the text of the novel "Weathering with You" with WordCloud
[Linux] I tried to verify the secure confirmation method of FQDN (CentOS7)
I tried to get the RSS of the top song of the iTunes store automatically
I tried to get the movie information of TMDb API with Python
I tried to display the altitude value of DTM in a graph
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to verify the result of A / B test by chi-square test
I tried to predict the behavior of the new coronavirus with the SEIR model.
I tried the asynchronous server of Django 3.0
I tried to summarize the umask command
I tried tensorflow for the first time
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
In search of the fastest FizzBuzz in Python
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I tried to solve the 2020 version of 100 language processing knocks [Chapter 3: Regular expressions 20 to 24]