|
1 | 1 | # functional_programming_vs_procedural_programming
|
2 |
| -A PHP comparison of the efficiency of functional programming vs procedural programming. |
| 2 | +The goal of this study was to compare the efficiency of functional programming vs procedural programming. |
| 3 | + |
| 4 | +An interesting introduction on this topic is <b>Adam Wathan book "Refactoring to Collections" (2016)</b>. |
| 5 | + |
| 6 | +The book contains 13 study cases where commonplace programming challenge can be solved using both |
| 7 | +functional programming (= "declarative programming" and procedural progamming (= "imperative programming"). |
| 8 | + |
| 9 | +A. Wathan book uses Laravel Collection class (Illuminate\Support\Collection) to solve the 13 challenges. |
| 10 | + |
| 11 | +You can read and/or download the 13 study cases here : |
| 12 | + |
| 13 | +https://drive.google.com/file/d/18QI1LkxP7MKxuh9d6F1KQl1uodr6N8G9/view?usp=sharing |
| 14 | + |
| 15 | +The code in the repository shows how to solve each challenge using both (1) declarative progamming methods and (2) imperative progamming methods. |
| 16 | + |
| 17 | +For each of the 13 study cases, I compared the speed at which a solution is obtained. (Note: 1 microsecond = 0.001 millisecond) |
| 18 | + |
| 19 | +To try to obtain meaningful results, I made sure each array contains at least 100 rows entries. |
| 20 | + |
| 21 | +Only the relevant Laravel files for the computations have been added to the repository. |
| 22 | + |
| 23 | +The results: |
| 24 | + |
| 25 | +<b>1. Pricing Lamps and Wallets</b> |
| 26 | + |
| 27 | +<b>imperative solution:</b> 7.5817108154297E-5 microseconds, <b>declarative solution:</b> 0.0013389587402344 microseconds |
| 28 | + |
| 29 | +<b>2. CSV Surgery 101</b> |
| 30 | + |
| 31 | +<b>imperative solution:</b> 6.0081481933594E-5 microseconds, <b>declarative solution:</b> 0.00060510635375977 microseconds |
| 32 | + |
| 33 | +<b>3. Binary to Decimal</b> |
| 34 | + |
| 35 | +<b>imperative solution:</b> 2.8610229492188E-6 microseconds, <b>declarative solution:</b> 8.082389831543E-5 microseconds |
| 36 | + |
| 37 | +<b>4. What's your Github Score</b> |
| 38 | + |
| 39 | +<b>imperative solution:</b> 1.0471658706665 microseconds, <b>declarative solution:</b> 0.23328900337219 microseconds |
| 40 | + |
| 41 | +<b>5. Formatting a Pull Request Comment</b> |
| 42 | + |
| 43 | +<b>imperative solution:</b> 0.00059890747070312 microseconds, <b>declarative solution:</b> 0.00013899803161621 microseconds |
| 44 | + |
| 45 | +<b>6. Stealing Mail</b> |
| 46 | + |
| 47 | +<b>imperative solution:</b> 0.00035881996154785 microseconds, <b>declarative solution:</b> 5.3882598876953E-5 microseconds |
| 48 | + |
| 49 | +<b>7. Choosing a Syntax Handler</b> |
| 50 | + |
| 51 | +<b>imperative solution:</b> 1.5020370483398E-5 microseconds, <b>declarative solution:</b> 5.6982040405273E-5 microseconds |
| 52 | + |
| 53 | +<b>8. Tagging on the Fly</b> |
| 54 | + |
| 55 | +<b>imperative solution:</b> 0.00051498413085938 microseconds, <b>declarative solution:</b> 0.0002892017364502 microseconds |
| 56 | + |
| 57 | +<b>9. Nitpicking a Pull Request</b> |
| 58 | + |
| 59 | +<b>imperative solution:</b> 0.0016169548034668 microseconds, <b>declarative solution:</b> 0.0016999244689941 microseconds |
| 60 | + |
| 61 | +<b>10. Comparing Monthly Revenue</b> |
| 62 | + |
| 63 | +<b>imperative solution:</b> 0.00088286399841309 microseconds, <b>declarative solution:</b> 0.00071811676025391 microseconds |
| 64 | + |
| 65 | +<b>11. Building a Lookup Table</b> |
| 66 | + |
| 67 | +<b>imperative solution:</b> 0.001635074615478 microseconds, <b>declarative solution:</b> 0.00046586990356445 microseconds |
| 68 | + |
| 69 | +<b>12. Transforming Form Input</b> |
| 70 | + |
| 71 | +<b>imperative solution:</b> 0.0038280487060547 microseconds, <b>declarative solution:</b> 2.5033950805664E-5 microseconds |
| 72 | + |
| 73 | +<b>13. Ranking a Competition</b> |
| 74 | + |
| 75 | +<b>imperative solution:</b> 0.0062758922576904 microseconds, <b>declarative solution:</b> 0.0056121349334717 microseconds |
| 76 | + |
| 77 | +<b>Conclusion:</b> |
| 78 | + |
| 79 | +It appears quite complicated to draw clear conclusions. Overall, the more array computations are involved, the greater the declarative solution edge over the imperative solution (Study cases #4,#11,#12 and #13). |
0 commit comments