Durante a apresentação do grande prof. Júlio Neves na BSD Day 2022 sobre shell, ele fez uma comparação interessante sobre shell ser dito lento.  E mostrou o seguinte slide:

Eu achei muito interessante o exemplo, mas resolvi testar ele pra valer.

Então testei não com 200 interações, mas com 20 milhões.  Daí sim a coisa fica mais interessante.

Fiz o mesmo script em shell:

#! /usr/bin/env bash

for ((i=1; i<20000000; i++)) {
  : > arq-shell
}

Em python:

#! /usr/bin/env python3

for i in range(20000000):
    with open("arq-python3", "w") as fd:
        None

em perl:

#! /usr/bin/env perl

for ($i=0;$i<20000000;$i++) {
  open(FD, ">arq-perl");
}

e finalmente em Go:

package main

import (
        "log"
        "os"
)

func main() {
        for i := 0; i < 20000000; i++ {
                fd, err := os.Create("arq-go")
                fd.Close()
                if err != nil {
                        log.Fatal(err)
                }
        }
}

Os resultados foram os seguintes:

  
helio@goosfraba /t/comparacao-shell> time ./20M-touch.sh
________________________________________________________
    Executed in  303.69 secs    fish           external 
    usr time  164.93 secs    0.00 micros  164.93 secs 
    sys time  133.79 secs  533.00 micros  133.79 secs 
    
helio@goosfraba /t/comparacao-shell> time./20M-touch.py
________________________________________________________ 
    Executed in  374.49 secs    fish           external 
    usr time  225.16 secs  623.00 micros  225.16 secs 
    sys time  143.90 secs  125.00 micros  143.90 secs 
    
helio@goosfraba /t/comparacao-shell> time ./20M-touch.pl
________________________________________________________ 
    Executed in  173.94 secs    fish           external 
    usr time   47.95 secs    1.05 millis   47.95 secs 
    sys time  122.10 secs    0.00 millis  122.10 secs
    
helio@goosfraba /t/comparacao-shell> time ./20M-touch 
________________________________________________________ 
    Executed in  147.80 secs    fish           external 
    usr time   45.82 secs  579.00 micros   45.82 secs 
    sys time  107.38 secs  113.00 micros  107.38 secs 
    

Eu achei os resultados um pouco miseráveis pra python.  Então re-escrevi a função como era feito desde o python 1.2 (usando 3.10.4):

#! /usr/bin/env python3

for i in range(20000000):
    fd = open("arq-python3", "w")
    fd.close()

e o resultado não melhorou muita coisa.

  
helio@goosfraba /t/comparacao-shell> time ./20M-touch.py
________________________________________________________
    Executed in  370.23 secs    fish           external
    usr time  218.59 secs  641.00 micros  218.59 secs
    sys time  146.02 secs  132.00 micros  146.02 secs
  

Acho que agora os números falam por si só sobre shell ser lento ou não.  Claro que pra coisas mais simples é bem mais fácil fazer em shell, mas isso não significa um desempenho melhor.

Tirando essa parte de desafio, a palestra foi espetacular.  Quem não viu, recomendo que assistam.

 

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.