Advertisement
fcamuso

Algoritmi lezione 3

Jan 25th, 2024 (edited)
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. //VERSIONE C++
  2.  
  3. #include <iostream>
  4. #include <chrono>
  5.  
  6.  
  7. using namespace std;
  8. string genera_stringa_casuale(int lunghezza) {
  9.     static const string alfabeto =
  10.         "0123456789"
  11.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12.         "abcdefghijklmnopqrstuvwxyz";
  13.  
  14.     string risultato="";
  15.  
  16.     for (int i = 0; i < lunghezza; ++i)
  17.         risultato += alfabeto[ rand()% alfabeto.length() ];
  18.  
  19.  
  20.     return risultato;
  21. }
  22.  
  23. const int QUANTI_ELEMENTI = 1000000;
  24. string v[QUANTI_ELEMENTI];
  25.  
  26. int main()
  27. {
  28.  
  29.     auto inizio  = chrono::high_resolution_clock::now();
  30.  
  31.     for (int i=0; i<QUANTI_ELEMENTI; i++)
  32.       v[i] = genera_stringa_casuale(50);
  33.  
  34.     auto fine = chrono::high_resolution_clock::now();
  35.  
  36.     auto durata = chrono::duration_cast<std::chrono::milliseconds>(fine - inizio);
  37.  
  38.     cout << "Durata: " << durata.count() << " millisecondi " << endl;
  39.  
  40.  
  41.  
  42.     return 0;
  43. }
  44.  
  45. ------------------------------------------------------------------------------------------------------------------------------
  46.  
  47.  
  48. //VERSIONE PHP
  49.  
  50. <!DOCTYPE html>
  51. <html lang="en">
  52. <head>
  53.   <meta charset="UTF-8">
  54.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  55.   <title>Document</title>
  56. </head>
  57. <body>
  58.   <?php
  59.     function generaStringaCasuale($lunghezza) {
  60.       $alfabeto =
  61.           "0123456789"
  62.           ."ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  63.           ."abcdefghijklmnopqrstuvwxyz";
  64.  
  65.       $risultato = "";
  66.  
  67.       for ($i = 0; $i < $lunghezza; ++$i)
  68.           $risultato .= $alfabeto[rand(0, strlen($alfabeto) - 1)];
  69.  
  70.       return $risultato;
  71.     }
  72.  
  73.     const QUANTI_ELEMENTI = 1000000;
  74.     $v = array();
  75.  
  76.     $inizio = microtime(true);
  77.  
  78.     for ($i = 0; $i < QUANTI_ELEMENTI; ++$i)
  79.       $v[$i] = generaStringaCasuale(50);
  80.  
  81.     $fine = microtime(true);
  82.  
  83.     $SecondiImpiegati = ($fine - $inizio);
  84.     echo "Ho impiegato (circa) $SecondiImpiegati \n";
  85.   ?>    
  86.  
  87. </body>
  88. </html>
  89. ------------------------------------------------------------------------------------------------------
  90.  
  91.  
  92. //VERSIONE JAVASCRIPT
  93.  
  94. <!DOCTYPE html>
  95. <html lang="en">
  96. <head>
  97.   <meta charset="UTF-8">
  98.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  99.   <title>Document</title>
  100. </head>
  101. <body>
  102.   <script>
  103.     function generaStringaCasuale(lunghezza) {
  104.     const alfabeto =
  105.         "0123456789" +
  106.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  107.         "abcdefghijklmnopqrstuvwxyz";
  108.  
  109.     let risultato = "";
  110.  
  111.     for (let i = 0; i < lunghezza; ++i)
  112.         risultato += alfabeto.charAt(Math.floor(Math.random() * alfabeto.length));
  113.  
  114.     return risultato;
  115.     }
  116.  
  117.       const QUANTI_ELEMENTI = 1000000;
  118.       const v = [];
  119.  
  120.       const inizio = Date.now();
  121.  
  122.       for (let i = 0; i < QUANTI_ELEMENTI; ++i)
  123.           v[i] = generaStringaCasuale(50);
  124.  
  125.       const fine = Date.now();
  126.  
  127.       const secondiImpiegati = (fine - inizio) / 1000;
  128.       alert("Ho impiegato (circa) " + secondiImpiegati + " secondi");
  129.  
  130.   </script>
  131. </body>
  132. </html>
  133. --------------------------------------------------------------------------------------------------
  134.  
  135. //VERSIONE C#
  136.  
  137. using System;
  138.  
  139. namespace ConsoleApp3
  140. {
  141.     using System;
  142.  
  143.     class Program
  144.     {
  145.         static string GeneraStringaCasuale(int lunghezza)
  146.         {
  147.             const string alfabeto =
  148.                     "0123456789" +
  149.                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
  150.                     "abcdefghijklmnopqrstuvwxyz";
  151.  
  152.             Random random = new Random();
  153.             string risultato = "";
  154.  
  155.             for (int i = 0; i < lunghezza; ++i)
  156.                 risultato += alfabeto[random.Next(0, alfabeto.Length)];
  157.  
  158.             return risultato;
  159.         }
  160.  
  161.         const int QUANTI_ELEMENTI = 1000000;
  162.         static string[] v = new string[QUANTI_ELEMENTI];
  163.  
  164.         static void Main()
  165.         {
  166.  
  167.             DateTime inizio = DateTime.Now;
  168.  
  169.             for (int i = 0; i < QUANTI_ELEMENTI; ++i)
  170.                 v[i] = GeneraStringaCasuale(50);
  171.  
  172.  
  173.             DateTime fine = DateTime.Now;
  174.  
  175.             TimeSpan tempoImpiegato = fine - inizio;
  176.             Console.WriteLine($"Ho impiegato (circa) {tempoImpiegato.TotalSeconds} secondi");
  177.         }
  178.     }
  179.  
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement