Skip to content

Instantly share code, notes, and snippets.

@gwy15

gwy15/693010.py Secret

Created July 25, 2020 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwy15/22616306f560f65d77b6ca23954e91a3 to your computer and use it in GitHub Desktop.
Save gwy15/22616306f560f65d77b6ca23954e91a3 to your computer and use it in GitHub Desktop.
import pytest
import random
from typing import List, Tuple
from functools import reduce
from operator import add
def shuffle_oneline(nums: List[int], n: int) -> List[int]:
return reduce(add, zip(nums[:n], nums[n:]))
def shuffle_iteration(nums: List[int], n: int) -> List[int]:
result = []
for i in range(n):
result.append(nums[i])
result.append(nums[n+i])
return result
# benchmarks with pytest-benchmark
# pytest --benchmark-min-time=0.1 --benchmark-min-rounds=10 <file>
def nums(n):
return random.choices(range(1_000), k=2*n)
def _test_one_line(n, benchmark):
benchmark(shuffle_oneline, nums=nums(n), n=n)
def _test_iteration(n, benchmark):
benchmark(shuffle_iteration, nums=nums(n), n=n)
# n=10
@pytest.mark.benchmark(group=10)
def test_one_line_10(benchmark):
benchmark(shuffle_oneline, nums=nums(10), n=10)
@pytest.mark.benchmark(group=10)
def test_iteration_10(benchmark):
benchmark(shuffle_iteration, nums=nums(10), n=10)
# n=100
@pytest.mark.benchmark(group=100)
def test_one_line_100(benchmark):
benchmark(shuffle_oneline, nums=nums(100), n=100)
@pytest.mark.benchmark(group=100)
def test_iteration_100(benchmark):
benchmark(shuffle_iteration, nums=nums(100), n=100)
# n=1000
@pytest.mark.benchmark(group=1000)
def test_one_line_1000(benchmark):
benchmark(shuffle_oneline, nums=nums(1000), n=1000)
@pytest.mark.benchmark(group=1000)
def test_iteration_1000(benchmark):
benchmark(shuffle_iteration, nums=nums(1000), n=1000)
# n=10_000
@pytest.mark.benchmark(group=10_000)
def test_one_line_10_000(benchmark):
benchmark(shuffle_oneline, nums=nums(10_000), n=10_000)
@pytest.mark.benchmark(group=10_000)
def test_iteration_10_000(benchmark):
benchmark(shuffle_iteration, nums=nums(10_000), n=10_000)
# n=100_000
@pytest.mark.benchmark(group=100_000)
def test_one_line_100_000(benchmark):
benchmark(shuffle_oneline, nums=nums(100_000), n=100_000)
@pytest.mark.benchmark(group=100_000)
def test_iteration_100_000(benchmark):
benchmark(shuffle_iteration, nums=nums(100_000), n=100_000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment