Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dot_product_timeit.py
- import timeit
- # Simulate large image data for image enhancing (e.g., 5 million pixel values)... or even whatever training
- vector_size = 5000000 # Adjust based on your system's capability
- vector1 = [i % 256 for i in range(vector_size)] # Pixel values 0-255
- vector2 = [(i + 50) % 256 for i in range(vector_size)] # Offset pixel values
- # Dot product implementation using generator and zip
- def dot_product(A, B):
- return sum(a * b for a, b in zip(A, B))
- # Basic arithmetic implementation with indexed loop
- def basic_arithmetic(A, B):
- result = 0
- for i in range(len(A)):
- result += A[i] * B[i]
- return result
- result_dot = dot_product(vector1, vector2)
- result_basic = basic_arithmetic(vector1, vector2)
- if result_dot == result_basic:
- print("Results are equal. Waiting for timeit comparison...")
- else:
- print("Results are NOT equal. Quitting...")
- 0/0
- def run_dot_product():
- dot_product(vector1, vector2)
- def run_basic_arithmetic():
- basic_arithmetic(vector1, vector2)
- # Performance comparison with reduced iterations for large data
- test_runs = 10 # Reduced due to large vector size
- dot_time = timeit.timeit(run_dot_product, number=test_runs)
- basic_time = timeit.timeit(run_basic_arithmetic, number=test_runs)
- print(f"Performance comparison for {vector_size} elements ({test_runs} runs):")
- print(f"Dot Product (optimized): {dot_time:.3f} seconds")
- print(f"Basic Arithmetic (loop): {basic_time:.3f} seconds")
- print(f"Dot Product was {basic_time/dot_time:.3f}x faster")
Advertisement
Comments
-
- import numpy as np
- import timeit
- vector_size = 1000000
- # Create NumPy arrays directly for better performance.
- vector1 = np.arange(vector_size, dtype=np.int32) % 256
- vector2 = (np.arange(vector_size, dtype=np.int32) + 50) % 256
- def run_numpy_dot():
- np.dot(vector1, vector2)
- # Time the NumPy dot product
- test_runs = 10
- numpy_time = timeit.timeit(run_numpy_dot, number=test_runs)
- print(f"NumPy dot product: {numpy_time:.3f} seconds")
Add Comment
Please, Sign In to add comment
Advertisement