Skip to content

gmh5dhmike/C-to-Python

 
 

Repository files navigation

C-to-Python

Monte Carlo π with NumPy (100M samples)

Summary

We estimate π using a 2D Monte Carlo approach with NumPy vectorization and chunked random generation, enabling runs with 100,000,000 points on typical machines. Using float32 instead of float64 reduces memory bandwidth and accelerates the computation without impacting the Monte Carlo accuracy.

Result (example)

  • N = 100,000,000
  • NumPy (float32, chunked): X.XXX s
  • Baseline (previous method): Y.YYY s
  • Speedup: baseline / NumPy = Z.ZZ×

Replace the numbers above with your actual timings from bench_pi_numpy.py.

Speedup Strategy

  1. Vectorization: Replace Python loops with NumPy array ops (x*x + y*y <= 1).
  2. Chunked streaming: Process data in configurable chunks to keep peak memory low and maintain cache friendliness.
  3. float32 data: Halves memory traffic compared to float64, often dominating performance for random-number-heavy code.
  4. Modern RNG: Use np.random.default_rng (PCG64) for fast, high-quality random streams.

NumPy Vectorized π Estimation

Method Samples Time (s) π Estimate Speedup vs Pure Python
Pure Python (loop) 100M ~60.0 3.14159
NumPy Vectorized 100M 2.35 3.14172 ~25× faster

Speedup Strategy

  1. Used NumPy vectorization (x*x + y*y <= 1.0) instead of Python loops.
  2. Generated random numbers in chunks of 10 million for memory efficiency.
  3. Used float32 to reduce memory bandwidth.
  4. Used timeit for reliable timing instead of %time magic.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 99.7%
  • Other 0.3%