Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a PHP script to compare time consumption between built in shuffle and seeded shuffle
<?php
$ans1 = json_decode("{ \"id\": 503732, \"question_id\": 129290, \"title\": \"Hemen hemen her gün yorgunum\", \"link\": null, \"background\": \"\", \"vote_count\": 757, \"created_at\": \"2018-01-30 15:06:05\", \"updated_at\": \"2018-01-30 15:45:24\", \"triggers_lead\": 0, \"cm_question_id\": 129294, \"triggers_display\": 0, \"has_right_answer\": 0, \"is_right_answer\": 0, \"voted\": 0 }");
$ans2 = json_decode("{ \"id\": 503733, \"question_id\": 129290, \"title\": \"Haftada en az 3-4 gün yorgunum\", \"link\": null, \"background\": \"\", \"vote_count\": 262, \"created_at\": \"2018-01-30 15:06:05\", \"updated_at\": \"2018-01-30 15:45:24\", \"triggers_lead\": 0, \"cm_question_id\": 129294, \"triggers_display\": 0, \"has_right_answer\": 0, \"is_right_answer\": 0, \"voted\": 0 }");
$ans3 = json_decode("{ \"id\": 503734, \"question_id\": 129290, \"title\": \"Haftada 1-2 gün yorgunum\", \"link\": null, \"background\": \"\", \"vote_count\": 199, \"created_at\": \"2018-01-30 15:06:05\", \"updated_at\": \"2018-01-30 15:45:24\", \"triggers_lead\": 0, \"cm_question_id\": 129294, \"triggers_display\": 0, \"has_right_answer\": 0, \"is_right_answer\": 0, \"voted\": 0 }");
$ans4 = json_decode("{ \"id\": 503735, \"question_id\": 129290, \"title\": \"Çok nadir yorgunluk hissediyorum\", \"link\": null, \"background\": \"\", \"vote_count\": 99, \"created_at\": \"2018-01-30 15:06:05\", \"updated_at\": \"2018-01-30 15:45:24\", \"triggers_lead\": 0, \"cm_question_id\": 129294, \"triggers_display\": 0, \"has_right_answer\": 0, \"is_right_answer\": 0, \"voted\": 0 }");
$ans5 = json_decode("{ \"id\": 503736, \"question_id\": 129290, \"title\": \"Hemen hemen hiç yorgun hissetmem\", \"link\": null, \"background\": \"\", \"vote_count\": 26, \"created_at\": \"2018-01-30 15:06:05\", \"updated_at\": \"2018-01-30 15:45:24\", \"triggers_lead\": 0, \"cm_question_id\": 129294, \"triggers_display\": 0, \"has_right_answer\": 0, \"is_right_answer\": 0, \"voted\": 0 }");
## random shuffle
$question1 = [ 'answers' => [ $ans1, $ans2, $ans3, $ans4, $ans5, ], ];
$time = microtime(True);
shuffle($question1['answers']);
$time = microtime(True) - $time;
echo "shuffle: ".$time."\n";
## seeded shuffle
$question2 = [ 'answers' => [ $ans1, $ans2, $ans3, $ans4, $ans5, ], ];
$requester_id = 10020;
if ((int) $requester_id > 0) { mt_srand("$requester_id"); }
# shuffle
$time2 = microtime(True);
for ($i = sizeof($question2['answers'])-1; $i > 0; $i--) {
$j = mt_rand(0, $i);
$tmp = $question2['answers'][$i];
$question2['answers'][$i] = $question2['answers'][$j];
$question2['answers'][$j] = $tmp;
}
$time2 = microtime(True) - $time2;
echo "mt_rand: $time2 \n";
/**
* Created by PhpStorm.
* User: freedrone
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment