Last active
May 19, 2017 07:58
-
-
Save SibTiger/82f389de52a993dffb064c934ec9607f to your computer and use it in GitHub Desktop.
This is an experiment to see which is faster: Command Shell, Python, LuaJIT, ASM, C, C#, or GO. Spoiler alert: CMD is the slowest!
This file contains 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
@ECHO OFF | |
REM =========================================================================================== | |
REM THE MONTYHALL EXPERIMENT | |
REM This is an experiment to see which is faster: Command Shell, Python, LuaJIT, ASM, C, C#, or D. | |
REM ORIGINAL CODE SOURCE TO MERGE: http://pastebin.com/HLrqjwn5 | |
REM ------------------------------------------------------------------------------------------- | |
REM Author of the 'converted' source code: Nicholas "Tiger" Gautier | |
REM Date: 21.July.2015 | |
REM | |
REM Using the original source code, I have converted it from C language to Batch for the Command | |
REM Shell. I tried my best to follow the original code, but testing was rather minimal. | |
REM ------------------------------------------------------------------------------------------- | |
REM Command Shell Time Was: ~2 weeks | |
REM Tested By: Tiger | |
REM Source Code: <PROVIDED BELOW> | |
REM | |
REM D (Single Threaded) Time Was: 25.827s | |
REM Tested By: Affliction | |
REM Source Code: http://pastebin.com/ty8SjZiM | |
REM | |
REM C (GCC Optimizations) Time Was: 14.970s | |
REM Tested By: Affliction | |
REM Source Code: http://pastebin.com/H6xxjgfH | |
REM | |
REM C (No GCC Optimizations) Time Was: 26.934s | |
REM Tested By: Affliction | |
REM Source Code: http://pastebin.com/H6xxjgfH | |
REM | |
REM ASM Time Was: 26.116s | |
REM Tested By: Affliction | |
REM Source Code: http://pastebin.com/VwTBNbsd | |
REM | |
REM Python Time Was: 70min 29.319s | |
REM Tested By: Affliction | |
REM Source Code: http://pastebin.com/ESM48w06 | |
REM | |
REM LuaJIT Time Was: 81seconds | |
REM Tested By: Tribeam | |
REM Source Code: http://pastebin.com/DvPPUhvQ | |
REM | |
REM C# Time Was: 59seconds | |
REM Tested By: Tribeam | |
REM Source Code: http://pastebin.com/zaceF5ri | |
REM | |
REM Go Time Was: ~100seconds [Warning: Time may not be accurate; different CPU was used] | |
REM Tested By: Affliction | |
REM Source Code: https://pastebin.com/TNX1MqvE | |
REM =========================================================================================== | |
TITLE MONTYHALL EXPERIMENT | |
GOTO :Main | |
:Main | |
REM Starting Time: | |
CALL :Time | |
SET timeStart=%ERRORLEVEL%& | |
ECHO Starting Time is: %timeStart%& | |
REM Now the Algorithm | |
CALL :Payload || (ECHO ERROR: Something went horribly wrong during the execution!) | |
REM Finished Time: | |
CALL :Time& | |
SET timeFinished=%ERRORLEVEL%& | |
ECHO Finished Time Is: %timeFinished%& | |
REM Time Difference; how much time did this take? | |
CALL :TimeDifference %timeStart% %timeFinished%& | |
ECHO Time Difference: %ERRORLEVEL%& | |
REM Finished: | |
PAUSE& | |
EXIT /B 0& | |
:Time | |
FOR /F "tokens=1-3 delims=:." %%a IN ("%TIME%") DO SET "cacheVar=%%a%%b%%c" | |
EXIT /B %cacheVar% | |
REM Requires two inputs: | |
REM %1 = Starting Time | |
REM %2 = Finished Time | |
:TimeDifference | |
SET /A timeDiff=%2-%1& | |
EXIT /B %timeDiff%& | |
REM ====================================================================================== | |
REM ====================================================================================== | |
REM ====================================================================================== | |
REM This is the main task used to see which language is faster at processing this task. | |
:Payload | |
REM ------------------ | |
REM EXTRAS | |
SET itercount_counter=0 | |
SET choseDoor_counter=0 | |
SET switchDoor_counter=0 | |
REM ------------------ | |
SET itercount=1000000000 | |
SET verboseItercount=0 | |
SET verbose=0 | |
CALL :Payload_Seed | |
SET g_Seed=1337 | |
SET switchCorrect=0 | |
SET initialCorrect=0 | |
SET wasInitialCorrect="PLACEHOLDER" | |
SET correct=0 | |
SET chosen=0 | |
SET opened=0 | |
SET switched=0 | |
ECHO Doing %itercount% iterations.... | |
REM Since Command Shell doesn't have a nice - proper FOR loop, we're going to have to get our hands dirty.... | |
CALL :Payload_Loop | |
CALL :Payload_FinalOutput | |
EXIT /B 0 | |
REM Requires one input: | |
REM %1 g_Seed | |
:Payload_Seed | |
SET /A cacheSeed=(214013 * %1 + 2531011) | |
SET /A cacheSeed=%cacheSeed% / 65536 | |
SET /A cacheSeed=%cacheSeed% %% 32767 | |
EXIT /B %cacheSeed% | |
REM ATTENTION: NASTY FORKING! This is recursively calling itself, but it shouldn't be a run-away operation. | |
:Payload_Loop | |
IF %itercount_counter% LEQ %itercount% (CALL :Payload_Loop_Process) ELSE (EXIT /B 0) | |
GOTO :Payload_Loop | |
:Payload_Loop_Process | |
REM Output the Itercount to the buffer [so we know where it is and what it's doing] | |
IF %verboseItercount% EQU 1 ECHO Iteration Counter is: %itercount_counter% target is %itercount% | |
REM Update the counter | |
SET /A itercount_counter=%itercount_counter%+1 | |
REM Fetch value for Correct: | |
CALL :Payload_Seed %g_Seed% | |
SET /A correct=%ERRORLEVEL% %% 3 | |
REM Fetch value for Chosen: | |
CALL :Payload_Seed %g_Seed% | |
SET /A chosen=%ERRORLEVEL% %% 3 | |
REM Decide which door to open. | |
CALL :Payload_Loop_Process_ChooseDoor_Loop | |
REM Decide which door to switch to. | |
CALL :Payload_Loop_Process_SwitchDoor_Loop | |
REM Is the var's Chosen and Correct equal to each other? | |
CALL :Payload_Loop_Process_Compare_ChosenCorrect | |
REM Is the var's switched and Correct equal to each other? | |
CALL :Payload_Loop_Process_Compare_SwitchedCorrect | |
REM Verbose Information [if enabled] | |
IF %verbose% EQU 1 CALL :Payload_Loop_Process_Verbose | |
REM Zero the temp data | |
CALL :Payload_Loop_Process_ZeroVars | |
REM Finished | |
EXIT /B 0 | |
:Payload_Loop_Process_ChooseDoor_Loop | |
IF %choseDoor_counter% LEQ 3 ( | |
CALL :Payload_Loop_Process_ChooseDoor_Process | |
) ELSE ( | |
EXIT /B 0 | |
) | |
IF %ERRORLEVEL% EQU 1 EXIT /B 0 | |
SET /A choseDoor_counter=%choseDoor_counter% + 1 | |
CALL :Payload_Loop_Process_ChooseDoor_Loop | |
EXIT /B 0 | |
:Payload_Loop_Process_ChooseDoor_Process | |
IF %choseDoor_counter% NEQ %correct% IF %choseDoor_counter% NEQ %chosen% (SET opened=%choseDoor_counter%& EXIT /B 1) | |
EXIT /B 0 | |
:Payload_Loop_Process_SwitchDoor_Loop | |
IF %switchDoor_counter% LEQ 3 ( | |
CALL :Payload_Loop_Process_SwitchDoor_Process | |
) ELSE ( | |
EXIT /B 0 | |
) | |
IF %ERRORLEVEL% EQU 1 EXIT /B 0 | |
SET /A switchDoor_counter=%switchDoor_counter% + 1 | |
CALL :Payload_Loop_Process_SwitchDoor_Loop | |
EXIT /B 0 | |
:Payload_Loop_Process_SwitchDoor_Process | |
IF %switchDoor_counter% NEQ %chosen% IF %switchDoor_counter% NEQ %opened% (SET switched=%switchDoor_counter%& EXIT /B 1) | |
EXIT /B 0 | |
:Payload_Loop_Process_Compare_ChosenCorrect | |
IF %chosen% EQU %correct% ( | |
SET /A initialCorrect=%initialCorrect% + 1 | |
IF %verbose% EQU 1 SET wasInitialCorrect=Initial choice correct. | |
) | |
EXIT /B 0 | |
:Payload_Loop_Process_Compare_SwitchedCorrect | |
IF %switched% EQU %correct% ( | |
SET /A switchCorrect=%switchCorrect% + 1 | |
IF %verbose% EQU 1 SET wasInitialCorrect=Switch Correct. | |
) | |
EXIT /B 0 | |
:Payload_Loop_Process_Verbose | |
ECHO Iteration %itercount_counter%: Choosing door %chosen%, opening door %opened%, switch to door %switched%, coorect door %correct% | |
IF "%wasInitialCorrect%" EQU "Initial choice correct." ( | |
ECHO Initial choice correct. | |
) ELSE ( | |
IF "%wasInitialCorrect%" EQU "Switch Correct." ECHO Switch Correct. | |
) | |
EXIT /B 0 | |
:Payload_Loop_Process_ZeroVars | |
SET correct=0 | |
SET chosen=0 | |
SET opened=0 | |
SET switched=0 | |
SET choseDoor_counter=0 | |
SET switchDoor_counter=0 | |
EXIT /B 0 | |
:Payload_FinalOutput | |
ECHO Initial choice correct: %initialCorrect% | |
ECHO Switch correct: %switchCorrect% | |
EXIT /B 0 | |
REM ====================================================================================== | |
REM ====================================================================================== | |
REM ====================================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment