← Back to NumPy Course | Chapter 1: Getting Started | Lesson 7 of 7

NumPy बनाम Python Speed

Same job को एक list loop और NumPy से time करना दिखाता है कि whole-array maths कितनी तेज़ है।

NumPy बनाम Python Speed

Vectorized NumPy code loops को optimized C में push करता है, per-element Python overhead avoid करते हुए। बड़े data पर speed-up commonly 10x से 100x है। आप इसे time module से खुद measure कर सकते हैं।

Note: Timing results machine से अलग होते हैं, इसलिए raw seconds की बजाय ratios compare करें।

उदाहरण: NumPy vs Python speed

python
import time
import numpy as np

n = 1_000_000
py_list = list(range(n))
arr = np.arange(n)

t0 = time.perf_counter()
py_sum = sum(x * 2 for x in py_list)
t_list = time.perf_counter() - t0

t0 = time.perf_counter()
np_sum = (arr * 2).sum()
t_np = time.perf_counter() - t0

print("Same answer:", py_sum == np_sum)
print("NumPy faster:", t_np < t_list)

# Output:
# Same answer: True
# NumPy faster: True
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. छोटे arrays benchmark करना जहाँ overhead dominate करे
  2. operation की बजाय array creation time करना
  3. यह मान लेना कि loops हमेशा ठीक हैं क्योंकि ये simpler हैं
चैप्टर सारांश
  • Vectorization Python loop overhead हटाता है
  • Speed-ups data size के साथ बढ़ते हैं
  • time.perf_counter से measure करें
  • Arrays पर explicit loops से बचें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.