Which is faster, Python shuffle or sample?

greeting

Nice to meet you, this is cells_comp. I, who was a ROM specialist, started Qiita at midnight. I'm sorry that the first article to commemorate is such a bad article, but I would like to post what I came up with for the time being.

What I tried

If you want to randomly sort the elements of list (l) in Python, think straightforwardly

--Sort by random.shuffle (l) --Sort by l2 = random.sample (l, len (l)) --Correctly, "random acquisition of the specified number from the list without duplication" is performed. --The specified number = all elements, so the result is a shuffled list.

I think that will come out. By the way, which of these two methods is faster? You may hear a voice saying ** Use numpy if you want to say speed **, but please forgive me.

Execution environment

Python 3.5.5 Intel® Core™ i7-6700 3.4GHz Windows10 (WSL) Ubuntu 16.04 LTS

Execution result

In the case of random.shuffle

The target list contains integers.

import random
import time

if __name__ == '__main__':
    l = [i for i in range(1000000)]
    start = time.time()
    random.shuffle(l)
    elapsed_time = time.time() - start
    print("elapsed_time:" + str(format(elapsed_time)) + "s")

result

elapsed_time:0.6656289100646973s

For random.sample

import random
import time

if __name__ == '__main__':
    l = [i for i in range(1000000)]
    start = time.time()
    l2 = random.sample(l,len(l))
    elapsed_time = time.time() - start
    print("elapsed_time:" + str(format(elapsed_time)) + "s")

result

elapsed_time:0.7895145416259766s

Yes, random.shuffle () is about 20% faster. After all, it is not optimal because it is originally a function for random extraction.

bonus

import random
import time
import numpy

if __name__ == '__main__':
    l = [i for i in range(1000000)]
    start = time.time()
    numpy.random.shuffle(l)
    elapsed_time = time.time() - start
    print("elapsed_time:" + str(format(elapsed_time)) + "s")

result

elapsed_time:0.1087648868560791s

***the end! Closed! that's all! Everyone disbanded! *** ***

in conclusion

Yes, this is my first post. I was so scared that I didn't post it until now, but when I try it, it's easy. It's a self-satisfying post, but I hope it helps even one person. I would like to continue posting in a timely manner. Well then, thank you for your cooperation.

Recommended Posts

Which is faster, Python shuffle or sample?
Which is better, PyPy or Python?
Python release cycle is faster!
[Python] Which is executed first, the class variable or __init__?
[Beginners are worried] Which is better, Ruby, PHP or Python?
Determining which OS is running Python
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
Python is easy
Rebooting vhost or Apache, which is often forgotten
Python closure sample
What is python
[Python] Which should be used, return or return None
Python is instance
What is Python
[Small story] In Python, i = i + 1 is slightly faster than i + = 1.
Python3 datetime is faster just by specifying the timezone
[Linux] End of process or job, which is better?
Raspberry Pi with Elixir, which is cooler than Python
python int is infinite
@ Is faster than dot
Python> list> extend () or + =
[Python] What is Pipeline ...
Python from or import
python autotest or sniffer
Ajax + Python + PostgreSQL sample
Memorandum @ Python OR Seminar
[Python] What is virtualenv
Python --Simple multi-thread sample
[Confrontation! Human power vs Python] After all, which is faster, solving the mathematics of the center test with Python or solving it by yourself?
Which is the most popular python visualization tool after all?
Analysis by Bayesian inference (1) ... Which is better, A or B?
Which should I study, R or Python, for data analysis?
[Python] Is the zero but non-empty object Truthy or Fallsy?
Which is faster, Python shuffle or sample?
[Python] pandas Code that is likely to be reused
Weird Python error message ——Is that code really executed?
Python release cycle is faster!