Skip to content

Instantly share code, notes, and snippets.

@CloudyPadmal
Created July 28, 2020 07:19
Show Gist options
  • Save CloudyPadmal/5baf0da378c5661858ef17c058faafbb to your computer and use it in GitHub Desktop.
Save CloudyPadmal/5baf0da378c5661858ef17c058faafbb to your computer and use it in GitHub Desktop.
Compare timing for loop merge operations using different methods
import time
import random
import numpy as np
A = [random.random() for i in range(65536)]
B = [random.random() for i in range(65536)]
C = [random.random() for i in range(65536)]
D = [random.random() for i in range(65536)]
E = [random.random() for i in range(65536)]
F = [random.random() for i in range(65536)]
a = time.time()
X = []; Y = []; Z = []
for i in range(65536):
X.append(A[i])
X.append(D[i])
Y.append(B[i])
Y.append(E[i])
Z.append(C[i])
Z.append(F[i])
Q = (X, Y, Z)
a = (time.time() - a)
b = time.time()
Q = ([*A, *D], [*B, *E], [*C, *F])
b = (time.time() - b)
c = time.time()
Q = (np.concatenate((A, D)), np.concatenate((B, E)), np.concatenate((C, F)))
c = time.time() - c
print('1. For loop: %f;\n2. Pointers: %f;\n3. Numpy : %f;' % (a, b, c))
@CloudyPadmal
Copy link
Author

1. For loop: 0.042167;
2. Pointers: 0.003162;
3. Numpy   : 0.020149;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment