Last active
August 29, 2015 14:05
-
-
Save ryanguill/5fbef7dd49738a261f9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfsetting requesttimeout="600" /> | |
| <cfscript> | |
| public array function i1 (required array a, required array b) { | |
| if (arrayLen(b) < arrayLen(a)) { | |
| var t = a; | |
| a = b; | |
| b = t; | |
| } | |
| var hs = createObject("java","java.util.HashSet").init(arrayLen(a)); | |
| for (var i in a) { | |
| hs.add(i); | |
| } | |
| var o = []; | |
| for (i in b) { | |
| if (hs.contains(i)) { | |
| arrayAppend(o, i); | |
| } | |
| } | |
| return o; | |
| } | |
| array function i2( array left, array right ){ | |
| var ht = {}; | |
| var result = []; | |
| if (arrayLen(right) < arrayLen(left)) { | |
| var t = left; | |
| left = right; | |
| right = t; | |
| } | |
| for (var i in right){ | |
| ht[i] = 1; | |
| } | |
| for (var i in left){ | |
| if ( structKeyExists( ht, i ) ){ | |
| arrayAppend( result, i ); | |
| } | |
| } | |
| return result; | |
| } | |
| //randomly generate two arrays of the same size with random integers | |
| arraySize = 100000; | |
| //how many runs | |
| runs = 20; | |
| results = arrayNew(2); | |
| resultsAvg = []; | |
| resultsAvg[1] = 0; | |
| resultsAvg[2] = 0; | |
| resultsMin = []; | |
| resultsMin[1] = 99999999; | |
| resultsMin[2] = 99999999; | |
| resultsMax = []; | |
| resultsMax[1] = 0; | |
| resultsMax[2] = 0; | |
| wins = []; | |
| wins[1] = 0; | |
| wins[2] = 0; | |
| a = b = []; | |
| for (i = 0; i < arraySize; i++) { | |
| arrayAppend(a, randRange(0,2^31-2)); | |
| } | |
| for (i = 0; i < arraySize-1; i++) { | |
| arrayAppend(b, randRange(0,2^31-2)); | |
| } | |
| for (i = 1; i <= runs; i++){ | |
| //pick a random number between 0 and 100, to randomize which function to call first | |
| if (randRange(0,100) < 50) { | |
| tc = getTickCount(); | |
| i1(a, b); | |
| results[i][1] = getTickCount() - tc; | |
| tc = getTickCount(); | |
| i2(a, b); | |
| results[i][2] = getTickCount() - tc; | |
| } else { | |
| tc = getTickCount(); | |
| i2(a, b); | |
| results[i][2] = getTickCount() - tc; | |
| tc = getTickCount(); | |
| i1(a, b); | |
| results[i][1] = getTickCount() - tc; | |
| } | |
| resultsAvg[1] += results[i][1]; | |
| resultsAvg[2] += results[i][2]; | |
| if (results[i][1] < resultsMin[1]) { | |
| resultsMin[1] = results[i][1]; | |
| } | |
| if (results[i][2] < resultsMin[2]) { | |
| resultsMin[2] = results[i][2]; | |
| } | |
| if (results[i][1] > resultsMax[1]) { | |
| resultsMax[1] = results[i][1]; | |
| } | |
| if (results[i][2] > resultsMax[2]) { | |
| resultsMax[2] = results[i][2]; | |
| } | |
| } | |
| resultsAvg[1] = resultsAvg[1] / runs; | |
| resultsAvg[2] = resultsAvg[2] / runs; | |
| </cfscript> | |
| <cfoutput> | |
| Runs: #runs# | |
| <table> | |
| <tr> | |
| <th> | |
| </th> | |
| <td> | |
| i1 | |
| </td> | |
| <td> | |
| i2 | |
| </td> | |
| </tr> | |
| <tr> | |
| <th> | |
| Avg: | |
| </th> | |
| <td> | |
| #resultsAvg[1]# | |
| </td> | |
| <td> | |
| #resultsAvg[2]# | |
| </td> | |
| </tr> | |
| <tr> | |
| <th> | |
| Min: | |
| </th> | |
| <td> | |
| #resultsMin[1]# | |
| </td> | |
| <td> | |
| #resultsMin[2]# | |
| </td> | |
| </tr> | |
| <tr> | |
| <th> | |
| Max: | |
| </th> | |
| <td> | |
| #resultsMax[1]# | |
| </td> | |
| <td> | |
| #resultsMax[2]# | |
| </td> | |
| </tr> | |
| </table> | |
| <table> | |
| <tr> | |
| <th> | |
| </th> | |
| <td> | |
| i1 | |
| </td> | |
| <td> | |
| i2 | |
| </td> | |
| </tr> | |
| <cfloop array="#results#" index="item"> | |
| <tr> | |
| <th> | |
| <cfif item[1] LT item[2]> | |
| <span style="font-weight:bold; color: blue;">I1</span> | |
| <cfset wins[1]++ /> | |
| <cfelseif item[2] LT item[1]> | |
| <span style="font-weight:bold; color: red;">I2</span> | |
| <cfset wins[2]++ /> | |
| <cfelse> | |
| <span style="font-weight:bold; color: green;">tie</span> | |
| </cfif> | |
| </th> | |
| <td> | |
| #item[1]# | |
| </td> | |
| <td> | |
| #item[2]# | |
| </td> | |
| </tr> | |
| </cfloop> | |
| </table> | |
| <hr /> | |
| wins: | |
| <table> | |
| <tr> | |
| <td> | |
| <span style="font-weight:bold; color: blue;">I1</span> | |
| </td> | |
| <td> | |
| <span style="font-weight:bold; color: red;">I2</span> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| #wins[1]# | |
| </td> | |
| <td> | |
| #wins[2]# | |
| </td> | |
| </tr> | |
| </table> | |
| </cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment