Wiki [PHP] Tester la vitesse de hashage des différents algorithmes

0
9
fév.
2020

Sommaire

[PHP] Tester la vitesse de hashage des différents algorithmes

Afin d'optimiser le code, il est toujours primordiale de trouver quelles sont les fonctions les plus rapide correspondant à un problème donné.

Les codes présentés ici vont permettre de tester facilement sur son propre serveur la vitesse de hashage des différents algorithmes et ce en monothread (un seul processus qui exécute tout) ou en multithread (un processus par fonction de hashage).

Monothread

Ce code fonctionne sur tout les systèmes d'exploitation.

    <?php
    $maxLoop=1000000;
    $total=time();
    $list=array();
    foreach (hash_algos() as $algo) {
        echo '<br /> =============> Algorithme : '.$algo.'<br />';
        $t=millitime();
        $i=0;
        while ( $i <= $maxLoop) {
            hash($algo, 'data');
            $i++;
        }
        $t=(millitime()-$t);
        echo ' - Loop : '.$maxLoop.' ;<br /> - Execution time : '.$t.'ms ('.($t/$maxLoop).'ms/hash) ;<br /> - Lenght : '.strlen(hash($algo, 'hello_world')).' ;<br /> - Example  : "hello_world" => '.hash($algo, 'hello_world');
        $list[$algo]=$t/$maxLoop;
    }
    echo '<br /><br /> <strong>Total execution time : '.(time()-$total).'s</strong>';
    asort($list);
    echo '<br /> - - - ms/hash => algo';
    foreach ($list as $algo => $ms) {
        echo '<br /> - - - '.$ms.' => '.$algo;
    }
                                        // initialize array (IMPORTANT)
    function millitime() {
      $microtime = microtime();
      $comps = explode(' ', $microtime);

      // Note: Using a string here to prevent loss of precision
      // in case of "overflow" (PHP converts it to a double)
      return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
    }
    ?>

MultiThread sur GNU/Linux

Attention : ce code ne fonctionne que sur les systèmes type Ubuntu, Debian. Il est très fortement recommandé d'avoir /tmp en ram.

    <?php
        $maxLoop=1000000;
        $file='/tmp/test'; // path and file prefix

        function millitime() {
          $microtime = microtime();
          $comps = explode(' ', $microtime);

          // Note: Using a string here to prevent loss of precision
          // in case of "overflow" (PHP converts it to a double)
          return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
        }

    function hashthis($algo, $maxLoop){
        $t=millitime();
        $i=0;
        while ( $i <= $maxLoop) {
            hash($algo, 'data');
            $i++;
        }
        $t=(millitime()-$t);
        return '<br /> =============> Algorithme : '.$algo.'<br /> - Loop : '.$maxLoop.' ;<br /> - Execution time : '.$t.'ms ('.($t/$maxLoop).'ms/hash) ;<br /> - Lenght : '.strlen(hash($algo, 'hello_world')).' ;<br /> - Example  : "hello_world" => '.hash($algo, 'hello_world');
    }

    if ($argv) { // if their is command parameter
    // sub process script actions :
            foreach ($argv as $value) {
                    if(preg_match('/algo:(.*)/', $value, $match)){
                            $algo=$match[1];
                    }
                    if(preg_match('/maxLoop:(.*)/', $value, $match)){
                            $maxLoop=$match[1];
                    }
            }
            $stream = fopen($file.'_'.$algo.'.txt', 'w+');
            fwrite($stream, hashthis($algo, $maxLoop));
            fwrite($stream, "\r\nstop"); // if your command already send a line break, remove \r\n
            fclose($stream);
            exit;
    }else{
    // main script action
            $timer=time(); // used for displaying duration time
                    // we creat our command list (important, the scrript need know how much command and return their is)
            $fileList=array();
            foreach(hash_algos() as $algo){
            // lunch sub process
                    $fileList[$algo]=array('file'=>urlencode($algo));
                    $algo=urlencode($algo);
                    system("php -f ".$_SERVER["SCRIPT_FILENAME"]." algo:".$algo." maxLoop:".$maxLoop." < /dev/null > /dev/null 2> /dev/null &");
                    usleep(250000);
            }
            // we w8t each sub process write 'stop' in it memory file
            $stop=true;
            $raw=array();
            $startTime=time();
            $maxActions=count(hash_algos());
            $timeout=250; // in sec
            while ($stop) {
                    sleep(1);
                    $numberActions=intval(shell_exec('tail -n 1 '.$file.'*.txt | grep "stop" | wc -l'));
                    if ( $maxActions==$numberActions || (time()-$startTime) >= $timeout ) {
                            $stop=false;
                            if ($maxActions==$numberActions) {
                                    shell_exec("sed -i '$ d' ".$file."*.txt"); // remove the "stop" in the last line in each file
                                    # all actions good, load
                                        // recover informations
                                    foreach ($fileList as $value) {
                                        $raw[]=file_get_contents($file.'_'.$value['file'].'.txt');
                                        usleep(200000); // w8t filesystem unlock files
                                        unlink($file.'_'.$value['file'].'.txt');
                                    }
                            }
                    }
            }
                    // finish, display infos
            echo 'Duration : '.(time()-$timer).' s<br />';
            //print_r($raw);
            foreach ($raw as $infos) {
                echo $infos;
            }
    }
    ?>

Résultat MonoThread sur ARM

=============> Algorithme : md2
- Loop : 1000000 ;
- Execution time : 6157ms (0.006157ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 219f1e1a03f35203d9b5d1edaa2bbff9
=============> Algorithme : md4
- Loop : 1000000 ;
- Execution time : 917ms (0.000917ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => b1540c4595efb6cd0055c2cc5f6d788d
=============> Algorithme : md5
- Loop : 1000000 ;
- Execution time : 884ms (0.000884ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 99b1ff8f11781541f7f89f9bd41c4a17
=============> Algorithme : sha1
- Loop : 1000000 ;
- Execution time : 1180ms (0.00118ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => e4ecd6fc11898565af24977e992cea0c9c7b7025
=============> Algorithme : sha224
- Loop : 1000000 ;
- Execution time : 1723ms (0.001723ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => 69c9392f54e5a0e0fff8945e9ed6475ef89236092a52b2005776912c
=============> Algorithme : sha256
- Loop : 1000000 ;
- Execution time : 1736ms (0.001736ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 35072c1ae546350e0bfa7ab11d49dc6f129e72ccd57ec7eb671225bbd197c8f1
=============> Algorithme : sha384
- Loop : 1000000 ;
- Execution time : 3979ms (0.003979ms/hash) ;
- Lenght : 96 ;
- Example : "hello_world" => 7f251a65acbe92af4c6a6d624c0860d9be77329e10e5beb3b9594f7916128cd95610a4d84e3a83a24a72362f6c8f9c46
=============> Algorithme : sha512/224
- Loop : 1000000 ;
- Execution time : 3976ms (0.003976ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => cd546e1ab3b0a2efa3a1488a4f9ec71368bfd95c918059171411827a
=============> Algorithme : sha512/256
- Loop : 1000000 ;
- Execution time : 3974ms (0.003974ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 3994440152c5d49d10d530e91a33e158b6971dce086f474c51feeea5fac5550f
=============> Algorithme : sha512
- Loop : 1000000 ;
- Execution time : 4022ms (0.004022ms/hash) ;
- Lenght : 128 ;
- Example : "hello_world" => 94f427efefa74c1230c3e93c35104dcbaa8ff71ba4537583ed83c0449d607c4e61b39c4c5eea5543e01d76a68e223da02b500530a82156625cb96ee8c8c80a85
=============> Algorithme : sha3-224
- Loop : 1000000 ;
- Execution time : 5317ms (0.005317ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => e24c066a49e260ba46a7b73d5d2374bfe86670be8ebbdf547bfce343
=============> Algorithme : sha3-256
- Loop : 1000000 ;
- Execution time : 5394ms (0.005394ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => fed30406b832b6c457e1e3605016eadfe7b57074c050e16ce2321de734ab29f4
=============> Algorithme : sha3-384
- Loop : 1000000 ;
- Execution time : 5500ms (0.0055ms/hash) ;
- Lenght : 96 ;
- Example : "hello_world" => d407e9fb45a350dce0d557f4d3d514f0a7db816163d3666b9e3ae61339a8b0a500129ef456fb9af7105c606599bc3ca1
=============> Algorithme : sha3-512
- Loop : 1000000 ;
- Execution time : 5587ms (0.005587ms/hash) ;
- Lenght : 128 ;
- Example : "hello_world" => 3d96f9b16a74980badc6aa05f8f102d781212744aee86e4c20f75c427f79ccea709487b2562c6e633607b53d0b247c389b88a9c3a9e032fadbdfe6ab9e00c528
=============> Algorithme : ripemd128
- Loop : 1000000 ;
- Execution time : 1448ms (0.001448ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 7ca29bdacaf287c93f96e0ecd36c9012
=============> Algorithme : ripemd160
- Loop : 1000000 ;
- Execution time : 1721ms (0.001721ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => c6b22ca8b206534aed9b5942b59d5159cc16e561
=============> Algorithme : ripemd256
- Loop : 1000000 ;
- Execution time : 1487ms (0.001487ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => f482a15c39b21ab09c9955fbd248f179704beca8672b95b59487f1474e4368c0
=============> Algorithme : ripemd320
- Loop : 1000000 ;
- Execution time : 1856ms (0.001856ms/hash) ;
- Lenght : 80 ;
- Example : "hello_world" => 473bf2ed405aa5b43beaad7ce28caad104913c347ff074addd0074088dd8766526d6d4603d5b2205
=============> Algorithme : whirlpool
- Loop : 1000000 ;
- Execution time : 4273ms (0.004273ms/hash) ;
- Lenght : 128 ;
- Example : "hello_world" => dc9812f562c463e1a56e8859ef4e0767757b2b5d41e3fc62fbcdebdf6a71fee19dd21e2308657e20e643dd8179c71b7d309b5b2af4c455a279609eb1d4ceaca7
=============> Algorithme : tiger128,3
- Loop : 1000000 ;
- Execution time : 1309ms (0.001309ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 3412a35e2b715dcf05274b83e3a8d461
=============> Algorithme : tiger160,3
- Loop : 1000000 ;
- Execution time : 1323ms (0.001323ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => 3412a35e2b715dcf05274b83e3a8d461518eb3aa
=============> Algorithme : tiger192,3
- Loop : 1000000 ;
- Execution time : 1483ms (0.001483ms/hash) ;
- Lenght : 48 ;
- Example : "hello_world" => 3412a35e2b715dcf05274b83e3a8d461518eb3aa69fdd874
=============> Algorithme : tiger128,4
- Loop : 1000000 ;
- Execution time : 1507ms (0.001507ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => a684e143ea477b6e044f86ee8c5cdf82
=============> Algorithme : tiger160,4
- Loop : 1000000 ;
- Execution time : 1524ms (0.001524ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => a684e143ea477b6e044f86ee8c5cdf824b2de298
=============> Algorithme : tiger192,4
- Loop : 1000000 ;
- Execution time : 1740ms (0.00174ms/hash) ;
- Lenght : 48 ;
- Example : "hello_world" => a684e143ea477b6e044f86ee8c5cdf824b2de298650bdf4c
=============> Algorithme : snefru
- Loop : 1000000 ;
- Execution time : 4801ms (0.004801ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 3e359d3d0416bebc586a4d149782943df2f18d3335f10151befbf0f1e50ce93c
=============> Algorithme : snefru256
- Loop : 1000000 ;
- Execution time : 4853ms (0.004853ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 3e359d3d0416bebc586a4d149782943df2f18d3335f10151befbf0f1e50ce93c
=============> Algorithme : gost
- Loop : 1000000 ;
- Execution time : 4251ms (0.004251ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 83e75079a85f89b4828a9b863f5f95e364498ad8adde42fe566a73d64cc81282
=============> Algorithme : gost-crypto
- Loop : 1000000 ;
- Execution time : 4365ms (0.004365ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 12663d5e3909912c9c53add5a8d95cfd90574ba86303de300f8bb7cbf8898860
=============> Algorithme : adler32
- Loop : 1000000 ;
- Execution time : 574ms (0.000574ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => 1b85049c
=============> Algorithme : crc32
- Loop : 1000000 ;
- Execution time : 539ms (0.000539ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => 8c0ae9d2
=============> Algorithme : crc32b
- Loop : 1000000 ;
- Execution time : 551ms (0.000551ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => f73eae91
=============> Algorithme : fnv132
- Loop : 1000000 ;
- Execution time : 560ms (0.00056ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => 585cb2f6
=============> Algorithme : fnv1a32
- Loop : 1000000 ;
- Execution time : 507ms (0.000507ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => e6c136a8
=============> Algorithme : fnv164
- Loop : 1000000 ;
- Execution time : 571ms (0.000571ms/hash) ;
- Lenght : 16 ;
- Example : "hello_world" => fe2326676546b716
=============> Algorithme : fnv1a64
- Loop : 1000000 ;
- Execution time : 593ms (0.000593ms/hash) ;
- Lenght : 16 ;
- Example : "hello_world" => 2de37268ea7d0f68
=============> Algorithme : joaat
- Loop : 1000000 ;
- Execution time : 538ms (0.000538ms/hash) ;
- Lenght : 8 ;
- Example : "hello_world" => 169d5817
=============> Algorithme : haval128,3
- Loop : 1000000 ;
- Execution time : 2615ms (0.002615ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 149b46dacad5d89e280551577b7fb516
=============> Algorithme : haval160,3
- Loop : 1000000 ;
- Execution time : 2579ms (0.002579ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => 14a0fa3ffcb40422681acf0a544006dc5c182ca0
=============> Algorithme : haval192,3
- Loop : 1000000 ;
- Execution time : 2645ms (0.002645ms/hash) ;
- Lenght : 48 ;
- Example : "hello_world" => 9b828664dc67fe06fb77412aed41af43ce434e11fe460763
=============> Algorithme : haval224,3
- Loop : 1000000 ;
- Execution time : 2634ms (0.002634ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => d27776ca2b634f1087d9ff609115a0c549f6380748332f644856aa31
=============> Algorithme : haval256,3
- Loop : 1000000 ;
- Execution time : 2713ms (0.002713ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => 4b7481bcc69bce4efabe22cbfb3b25bb7499a2455075cd5bdb01ec4552000c05
=============> Algorithme : haval128,4
- Loop : 1000000 ;
- Execution time : 3404ms (0.003404ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => e71d14a73f7fc3158ff7e351f2d22773
=============> Algorithme : haval160,4
- Loop : 1000000 ;
- Execution time : 3404ms (0.003404ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => 0ec09eaf9f6fbaca69f7816a42f3949bae990440
=============> Algorithme : haval192,4
- Loop : 1000000 ;
- Execution time : 3447ms (0.003447ms/hash) ;
- Lenght : 48 ;
- Example : "hello_world" => cc7d242d1633c7cb4db616c7483f31b4615690e8148ba7fc
=============> Algorithme : haval224,4
- Loop : 1000000 ;
- Execution time : 3428ms (0.003428ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => 7b87af3623bf1385bd1f6e78427d2308532df8b8e71846b905fdc897
=============> Algorithme : haval256,4
- Loop : 1000000 ;
- Execution time : 3462ms (0.003462ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => c22d98db6bee1911537562db095901e19e1617fe6f637e5bd79c5ceb1514d0de
=============> Algorithme : haval128,5
- Loop : 1000000 ;
- Execution time : 4015ms (0.004015ms/hash) ;
- Lenght : 32 ;
- Example : "hello_world" => 9bb26ab7fb13542f3706acf3d765d610
=============> Algorithme : haval160,5
- Loop : 1000000 ;
- Execution time : 4033ms (0.004033ms/hash) ;
- Lenght : 40 ;
- Example : "hello_world" => fbb620633f34b19d755233f29e34d3613db44291
=============> Algorithme : haval192,5
- Loop : 1000000 ;
- Execution time : 4060ms (0.00406ms/hash) ;
- Lenght : 48 ;
- Example : "hello_world" => bc1054bd5a84c9d1de988973b6a74e1199090f8dacbcb2e6
=============> Algorithme : haval224,5
- Loop : 1000000 ;
- Execution time : 4058ms (0.004058ms/hash) ;
- Lenght : 56 ;
- Example : "hello_world" => 78d4e7a0751428085e1b64c2b4d4ed42cc50d7425e27bd33f95b54d9
=============> Algorithme : haval256,5
- Loop : 1000000 ;
- Execution time : 4069ms (0.004069ms/hash) ;
- Lenght : 64 ;
- Example : "hello_world" => eec53eea6e76e919baddf8f8898e730df27c719c5ba736eaafac1f4d8ffbc7b2

Total execution time : 143s
- - - ms/hash => algo
- - - 0.000507 => fnv1a32
- - - 0.000538 => joaat
- - - 0.000539 => crc32
- - - 0.000551 => crc32b
- - - 0.00056 => fnv132
- - - 0.000571 => fnv164
- - - 0.000574 => adler32
- - - 0.000593 => fnv1a64
- - - 0.000884 => md5
- - - 0.000917 => md4
- - - 0.00118 => sha1
- - - 0.001309 => tiger128,3
- - - 0.001323 => tiger160,3
- - - 0.001448 => ripemd128
- - - 0.001483 => tiger192,3
- - - 0.001487 => ripemd256
- - - 0.001507 => tiger128,4
- - - 0.001524 => tiger160,4
- - - 0.001721 => ripemd160
- - - 0.001723 => sha224
- - - 0.001736 => sha256
- - - 0.00174 => tiger192,4
- - - 0.001856 => ripemd320
- - - 0.002579 => haval160,3
- - - 0.002615 => haval128,3
- - - 0.002634 => haval224,3
- - - 0.002645 => haval192,3
- - - 0.002713 => haval256,3
- - - 0.003404 => haval128,4
- - - 0.003404 => haval160,4
- - - 0.003428 => haval224,4
- - - 0.003447 => haval192,4
- - - 0.003462 => haval256,4
- - - 0.003974 => sha512/256
- - - 0.003976 => sha512/224
- - - 0.003979 => sha384
- - - 0.004015 => haval128,5
- - - 0.004022 => sha512
- - - 0.004033 => haval160,5
- - - 0.004058 => haval224,5
- - - 0.00406 => haval192,5
- - - 0.004069 => haval256,5
- - - 0.004251 => gost
- - - 0.004273 => whirlpool
- - - 0.004365 => gost-crypto
- - - 0.004801 => snefru
- - - 0.004853 => snefru256
- - - 0.005317 => sha3-224
- - - 0.005394 => sha3-256
- - - 0.0055 => sha3-384
- - - 0.005587 => sha3-512
- - - 0.006157 => md2

  • # Commentaire supprimé

    Posté par  . Évalué à -1 (+0/-0). Dernière modification le 28 décembre 2020 à 11:51.

    Ce commentaire a été supprimé par l’équipe de modération.

Envoyer un commentaire

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.