{"id":17946493,"url":"https://github.com/efremropelato/pi_resolution","last_synced_at":"2026-02-08T02:33:16.774Z","repository":{"id":259688386,"uuid":"879208132","full_name":"efremropelato/pi_resolution","owner":"efremropelato","description":"Benchmarking between Python, Ruby, Node.js, Golang, Java and Rust for calculating the value of Pi, using MonteCarlo method","archived":false,"fork":false,"pushed_at":"2024-11-03T14:41:42.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-03-30T17:11:09.482Z","etag":null,"topics":["benchmark","golang","java","math","monte-carlo-simulation","nodejs","pi","python","ruby","rust"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/efremropelato.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-27T09:53:35.000Z","updated_at":"2024-11-03T14:41:44.000Z","dependencies_parsed_at":"2024-10-27T11:09:30.486Z","dependency_job_id":"e18498b0-006c-43db-acc6-ad706bc3c580","html_url":"https://github.com/efremropelato/pi_resolution","commit_stats":null,"previous_names":["efremropelato/pi_resolution"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efremropelato%2Fpi_resolution","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efremropelato%2Fpi_resolution/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efremropelato%2Fpi_resolution/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efremropelato%2Fpi_resolution/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/efremropelato","download_url":"https://codeload.github.com/efremropelato/pi_resolution/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251723276,"owners_count":21633120,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmark","golang","java","math","monte-carlo-simulation","nodejs","pi","python","ruby","rust"],"created_at":"2024-10-29T07:05:52.989Z","updated_at":"2026-02-08T02:33:11.755Z","avatar_url":"https://github.com/efremropelato.png","language":"Java","readme":"# Metodo Montecarlo\n\n## Benchmarking, tra Python, Ruby, Nodejs, Golang, Java e Rust, per la risoluzione del valore di $\\Pi$ utilizzando il metodo Monte Carlo.\n\n\u003e ### Prerequisiti\n\u003e\n\u003e Devono essere installati:\n\u003e\n\u003e - Python ver. 3.12.4 o successiva\n\u003e - Ruby ver. 3.3.5 o successiva\n\u003e - Nodejs ver. 20.18.0 o successiva\n\u003e - Golang ver. 1.22.4 o successiva\n\u003e - Rust ver. 1.82.0 o successiva\n\u003e - Java ver. 1.8.0_422 o successiva\n\n### Confronto efficienza\n\nUtilizzando Python, Ruby, Nodejs, Golang, Java e Rust sono stati implementati quattro script con lo stesso algoritmo di risoluzione del valore di $\\Pi$: per nessuno di essi è stato utilizzato parallelismi o multithreading per ottimizzazione delle prestazioni.\nAlla fine dell'esecuzione, ogni script restituisce il tempo complessivo dell'elaborazione.\n\n### Esecuzione\n\nE' possibile eseguire tutti gli script in seguenza, tramite lo script `run.sh \u003cnumero interazioni\u003e` o `run.bat \u003cnumero interazioni\u003e`, oppure eseguirli singolarmente con:\n\n- Python\n\n  ```sh\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  python3 ./PY/main.py\n  ```\n\n- Ruby\n\n  ```sh\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  ruby ./RB/main.rb\n  ```\n\n- Nodejs\n\n  ```sh\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  node ./JS/index.js\n  ```\n\n- Go\n\n  ```sh\n  go build -o ./GO/main ./GO/main.go\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  ./GO/main\n  ```\n\n- Rust\n\n  ```sh\n  cd RS\n  cargo build --release -q\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  ./target/release/main\n  ```\n\n- Java\n\n  ```sh\n  cd JV\n  javac ./Main.java\n  export PI_SIMULATIONS=\u003cnumero interazione\u003e\n  java Main \u0026\u0026 cd ..\n  ``` ```\n  ```\n\n#### Hyperfine\n\nPer eseguire `./hyperfine.sh`, si deve installare il binario: [Hyperfine installation](https://github.com/sharkdp/hyperfine/tree/master?tab=readme-ov-file#installation)\n\n### Risultati\n\nUtilizzando un **Intel® Core™ i5-6500 e 32Gb** i risultati, misurati in secondi, sono:\n\n|            | ver.        | 1.000                       | 5.000                      | 1.000.000                  | 5.000.000                    |\n| ----------:|:-----------:|:---------------------------:|:--------------------------:|:--------------------------:|:----------------------------:|\n| **Python** | *3.12.4*    | 0,00017\u003cbr\u003e [739,13%]       | 0,000806\u003cbr\u003e [680,92%]     | 0,171172\u003cbr\u003e [1022,83%]    | 0,871845\u003cbr\u003e [936,66%]       |\n| **Ruby**   | *3.3.5*     | 0,003380644\u003cbr\u003e [14698,45%] | 0,01664606\u003cbr\u003e [14062,85%] | 3,185276161\u003cbr\u003e [19033,5%] | 16,004425921\u003cbr\u003e [17194,31%] |\n| **Nodejs** | *20.18.0*   | **\\***       | 0,001\u003cbr\u003e [844,81%]        | 0,03\u003cbr\u003e [179,26%]         | 0,135\u003cbr\u003e [145,03%]          |\n| **Golang** | *1.22.4*    | **0,000023\u003cbr\u003e [100%]**     | 0,000132\u003cbr\u003e [111,51%]     | 0,022069\u003cbr\u003e [131,87%]     | 0,119983\u003cbr\u003e [128,9%]        |\n| **Rust**   | *1.82.0*    | 0,000030641\u003cbr\u003e [133,22%]   | **0,000118369\u003cbr\u003e [100%]** | **0,016735104\u003cbr\u003e [100%]** | **0,093079769\u003cbr\u003e [100%]**   |\n| **Java**   | *1.8.0_422* | 0,001604\u003cbr\u003e [6973,91%]     | 0,004325\u003cbr\u003e [3653,82%]    | 0,050392\u003cbr\u003e [301,11%]     | 0,224448\u003cbr\u003e [241,13%]       |\n\nUtilizzando un **Apple® M2 e 16Gb** i risultati, misurati in secondi, sono:\n\n|            | ver.      | 1.000                     | 5.000                   | 1.000.000                | 5.000.000                 |\n| ----------:| --------- |:-------------------------:|:-----------------------:|:------------------------:|:-------------------------:|\n| **Python** | *3.12.4*  | 0,000101\u003cbr\u003e [721,43%]    | 0,000489\u003cbr\u003e [444,55%]  | 0,100177\u003cbr\u003e [778,62%]   | 0,529444\u003cbr\u003e [788,34%]    |\n| **Ruby**   | *3.3.5*   | 0,000484\u003cbr\u003e [3457,15%]   | 0,002548\u003cbr\u003e [2316,37%] | 0,545488\u003cbr\u003e [4239,77%]  | 2,631342\u003cbr\u003e [3918,02%]   |\n| **Nodejs** | *20.18.0* | **\\***     | 0,001\u003cbr\u003e [909,1%]      | 0,036\u003cbr\u003e [279,81%]      | 0,11\u003cbr\u003e [163,79%]        |\n| **Golang** | *1.22.4*  | **0,000014\u003cbr\u003e [100%]**   | 0,000182\u003cbr\u003e [165,46%]  | **0,012866\u003cbr\u003e [100%]**  | **0,06716\u003cbr\u003e [100%]**    |\n| **Rust**   | *1.82.0*  | 0,000025167\u003cbr\u003e [179,77%] | **0,00011\u003cbr\u003e [100%]**  | 0,022051792\u003cbr\u003e [171,4%] | 0,115934333\u003cbr\u003e [172,63%] |\n| **Java**   | *17.0.12* | 0,000559\u003cbr\u003e [3992,86%]   | 0,000761\u003cbr\u003e [691,82%]  | 0,042703\u003cbr\u003e [331,91%]   | 0,221541\u003cbr\u003e [329,88%]    |\n\n\u003e **\\*** Nodejs non misura frazioni del millisecondo\n\n### Algoritmo per risoluzione $\\Pi$\n\nIl metodo \"Monte Carlo\" è una stategia di risoluzione di problemi che utilizza la statistica: se la probabilità di un certo evento è P possiamo simulare in maniera random questo evento e ottenere P facendo:\n\n`P = (numero di volte in cui il nostro evento è avvenuto)/(simulazioni totali)`.\n\nVediamo come applicare questa strategia per ottenere un'approssimazione di pi greco $\\Pi$.\n\nData un cerchio di raggio 1, esso può essere inscritto in un quadrato di lato 2.\n\nGuardiamo solamente ad uno spicchio del cerchio:\n\n![PI_greco](./pi.png)\n\nIn questo modo sappiamo che l'area del quadrato in blu è 1 e l'area dell'area rossa invece è $\\Pi$/4.\n\nSe generiamo N numeri random all'interno del quadrato il numero di punti che cadono nel cerchio M diviso il numero totale di numeri generati N dovrà approssimare appunto l'area del cerchio e quindi $\\Pi$/4.\n\nIn sostanza otterremo $\\Pi$ = 4 \\* M / N.\n\nMaggiore sarà il numero di punti generati più precisa sarà l'approssimazione di $\\Pi$.\n\nChiaramente ogni esecuzione darà un valore leggermente diverso ma aumentando il numero di punti possiamo \"stabilizzare\" un numero di cifre decimali a piacere. Osserviamo come l'implementazione del random sia determinante per questo calcolo: se i double non fossero generati in maniera uniforme tra 0 e 1 il valore di pi risulterebbe sbagliato!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefremropelato%2Fpi_resolution","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fefremropelato%2Fpi_resolution","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefremropelato%2Fpi_resolution/lists"}