0
0
NumPydata~10 mins

NumPy array vs Python list performance - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - NumPy array vs Python list performance
Create Python list with numbers
Create NumPy array with same numbers
Measure time for sum with Python list
Measure time for sum with NumPy array
Compare times
Show which is faster
We create a list and a NumPy array with the same numbers, measure how long it takes to sum each, then compare the times to see which is faster.
Execution Sample
NumPy
import numpy as np
import time

lst = list(range(1000000))
arr = np.array(lst)

start = time.time()
sum_lst = sum(lst)
end = time.time()
print(end - start)

start = time.time()
sum_arr = np.sum(arr)
end = time.time()
print(end - start)
This code sums one million numbers using a Python list and a NumPy array, timing each operation.
Execution Table
StepActionData StructureOperationTime Taken (seconds)Result
1Create listPython listStore 1,000,000 integersN/AList of 1,000,000 integers
2Create arrayNumPy arrayConvert list to arrayN/ANumPy array of 1,000,000 integers
3Sum listPython listsum(lst)0.080499999500000
4Sum arrayNumPy arraynp.sum(arr)0.010499999500000
5Compare timesN/ACheck which is fasterN/ANumPy sum is faster
💡 Finished timing both sum operations and compared results
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
lstundefinedList with 1,000,000 integersList unchangedList unchangedList unchangedList unchanged
arrundefinedundefinedNumPy array with 1,000,000 integersNumPy array unchangedNumPy array unchangedNumPy array unchanged
sum_lstundefinedundefinedundefined499999500000499999500000499999500000
sum_arrundefinedundefinedundefinedundefined499999500000499999500000
Key Moments - 3 Insights
Why is the NumPy sum operation faster than the Python list sum?
NumPy uses optimized C code and vectorized operations internally, which makes summing arrays much faster than Python's built-in sum that iterates element by element (see execution_table rows 3 and 4).
Does the sum result differ between the list and the NumPy array?
No, both produce the same sum result (499999500000), confirming correctness while showing performance difference (see execution_table rows 3 and 4).
Why do we convert the list to a NumPy array before summing?
NumPy functions operate on NumPy arrays, not Python lists. Conversion allows us to use NumPy's fast methods (see execution_table row 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the time taken to sum the Python list?
A0.010 seconds
B1.000 seconds
C0.080 seconds
D0.000 seconds
💡 Hint
Check the 'Time Taken (seconds)' column at Step 3 in the execution_table.
At which step is the NumPy array created from the list?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Data Structure' columns in the execution_table for when the array is created.
If the list size doubled, how would the sum times likely change?
ABoth times would roughly double
BPython list sum time would double, NumPy sum time stays the same
CNumPy sum time would double, Python list sum time stays the same
DBoth times would stay the same
💡 Hint
Consider that both sums iterate over all elements; see variable_tracker for size impact.
Concept Snapshot
NumPy arrays store data in contiguous memory, enabling fast operations.
Python lists store references, making operations slower.
Use np.sum() for fast summing of arrays.
Convert lists to arrays to gain speed.
NumPy is faster for large numeric data.
Timing shows NumPy sum is much quicker than Python sum.
Full Transcript
We start by creating a Python list with one million numbers. Then, we convert this list into a NumPy array. Next, we measure how long it takes to sum all numbers in the Python list using the built-in sum function. After that, we measure the time to sum the same numbers using NumPy's sum function on the array. We find that NumPy's sum is much faster. Both sums produce the same result, confirming correctness. This shows that NumPy arrays are more efficient for numeric operations than Python lists.