예제 #1
0
import random
import numpy as np
from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)


def add_python(Z1, Z2):
    return [z1 + z2 for (z1, z2) in zip(Z1, Z2)]


def add_numpy(Z1, Z2):
    return np.add(Z1, Z2)


Z1 = random.sample(range(1000), 100)
Z2 = random.sample(range(1000), 100)

timeit("add_python(Z1, Z2)", globals())
timeit("add_numpy(Z1, Z2)", globals())
예제 #2
0
import numpy as np
from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)

Z = np.ones(4 * 1000000,
            np.float32)  # create an array of size 4*10000000 np.float32

print("np.float16:")
# time required to view array as np.float16
timeit("Z.view(np.float16)[...] = 0", globals())

print("np.int16:")
# time required to view array as np.int16
timeit("Z.view(np.int16)[...] = 0", globals())

print("np.int32:")
# time required to view array as np.int32
timeit("Z.view(np.int32)[...] = 0", globals())

print("np.float32:")
# time required to view array as np.float32
timeit("Z.view(np.float32)[...] = 0", globals())

print("np.int64:")
# time required to view array as np.int64
timeit("Z.view(np.int64)[...] = 0", globals())

print("np.float64:")
# time required to view array as np.float64
timeit("Z.view(np.float64)[...] = 0", globals())

print("np.complex128:")
예제 #3
0
from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)
import numpy as np


def random_walk_fastest(n=1000):
    # No 's' in NumPy choice (Python offers choice & choices)
    steps = np.random.choice([-1, +1], n)
    return np.cumsum(
        steps)  # return the cumulative sum of the steps along a given axis.


walk = random_walk_fastest(1000)
timeit("random_walk_fastest(n=1000)", globals())
# calculates the total loops and time per loop
예제 #4
0
파일: main.py 프로젝트: hvn2001/LearnPython
from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)
from itertools import accumulate  # get accumulate function from built accumulate module
import random


def random_walk_faster(n=1000):
    steps = random.choices([-1, +1], k=n)
    return [0] + list(accumulate(steps))  # get the total number of steps


walk = random_walk_faster(1000)
timeit("random_walk_faster(n=10000)",
       globals())  # calculates the total loops and time per loop
예제 #5
0
        Z = Z[I]
        Xi, Yi = Xi[I], Yi[I]
        C = C[I]
    return Z_.T, N_.T


if __name__ == '__main__':
    from matplotlib import colors
    import matplotlib.pyplot as plt
    from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)

    # Benchmark
    xmin, xmax, xn = -2.25, +0.75, int(3000 / 3)
    ymin, ymax, yn = -1.25, +1.25, int(2500 / 3)
    maxiter = 200
    timeit("mandelbrot_numpy2(xmin, xmax, ymin, ymax, xn, yn, maxiter)",
           globals())

    # Visualization
    xmin, xmax, xn = -2.25, +0.75, int(3000 / 2)
    ymin, ymax, yn = -1.25, +1.25, int(2500 / 2)
    maxiter = 20
    horizon = 2.0**40
    log_horizon = np.log(np.log(horizon)) / np.log(2)
    Z, N = mandelbrot(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)

    # Normalized recount as explained in:
    # http://linas.org/art-gallery/escape/smooth.html
    M = np.nan_to_num(N + 1 - np.log(np.log(abs(Z))) / np.log(2) + log_horizon)

    dpi = 72
    width = 10
예제 #6
0
import itertools as it
from PythonNumpy.tools import timeit


def solution_2():
    # Itertools
    # 14641 (=11*11*11*11) iterations & tests
    return [(i, j, k, l) for i, j, k, l in it.product(range(11), repeat=4)
            if i + j + k + l == 10]


timeit("solution_2()", globals())
예제 #7
0
파일: main.py 프로젝트: hvn2001/LearnPython
from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)
import random


class RandomWalker:
    def __init__(self):
        self.position = 0

    def walk(self, n):  # walk method
        self.position = 0
        for i in range(n):
            yield self.position
            self.position += 2 * random.randint(0, 1) - 1
            # returns current position after each random step


walker = RandomWalker()  # make instance of class walk
walk = [position for position in walker.walk(1000)]  # call the walk function

walker = RandomWalker()
timeit("[position for position in walker.walk(n=10000)]", globals())
# calculates the  total loops and time per loop
예제 #8
0
def test(X, Y):
    timeit("Z=X + 2.0*Y", globals())  # time taken by Z=X + 2.0*Y
    timeit("Z = X + 2*Y", globals())  # time taken by Z=X + 2*Y
    timeit("np.add(X, Y, out=X); np.add(X, Y, out=X)", globals())
예제 #9
0
파일: main.py 프로젝트: hvn2001/LearnPython
        Z = Z[I]
        Xi, Yi = Xi[I], Yi[I]
        C = C[I]
    return Z_.T, N_.T


if __name__ == '__main__':
    from matplotlib import colors
    import matplotlib.pyplot as plt
    from PythonNumpy.tools import timeit  # get time it from tools.py(custom module)

    # Benchmark
    xmin, xmax, xn = -2.25, +0.75, int(3000 / 3)
    ymin, ymax, yn = -1.25, +1.25, int(2500 / 3)
    maxiter = 200
    timeit("mandelbrot_python(xmin, xmax, ymin, ymax, xn, yn, maxiter)", globals())

    # Visualization
    xmin, xmax, xn = -2.25, +0.75, int(3000 / 2)
    ymin, ymax, yn = -1.25, +1.25, int(2500 / 2)
    maxiter = 20
    horizon = 2.0 ** 40
    log_horizon = np.log(np.log(horizon)) / np.log(2)
    Z, N = mandelbrot(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)

    # Normalized recount as explained in:
    # http://linas.org/art-gallery/escape/smooth.html
    M = np.nan_to_num(N + 1 - np.log(np.log(abs(Z))) / np.log(2) + log_horizon)

    dpi = 72
    width = 10