Timing Your First Speedup
Measuring GPU vs CPU on the same task.
Timing Your First Speedup is a free CUDA Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Now Measure the Win
You have a correct kernel, so the fun question is how much faster it is than the CPU. Measuring turns a guess into a real number. ⏱️
Time the CPU Baseline
Start with a fair baseline: time the same add running in a plain CPU loop. You need something to compare the GPU against.
Don't Time with the Clock Wrong
Avoid timing across a cudaMemcpy you forgot to wait for. The launch is asynchronous, so naive timers can report nonsense.
Use CUDA Events
The right tool is a pair of cudaEvent objects. They record GPU timestamps directly on the device for accurate kernel timing.
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);Bracket the Kernel
Record start just before the launch and stop just after. The events queue in the stream alongside your kernel.
cudaEventRecord(start);
vecAdd<<<blocks, threads>>>(d_A, d_B, d_C, n);
cudaEventRecord(stop);Wait, Then Read
Call cudaEventSynchronize on stop so the CPU waits for the GPU. Only then is the elapsed time ready to read.
cudaEventSynchronize(stop);
float ms = 0;
cudaEventElapsedTime(&ms, start, stop);Warm Up First
The very first launch pays one-time setup costs. Run a throwaway warm-up launch before timing so those costs do not skew your number.
Average Several Runs
A single sample is noisy, so time the kernel a few times and take the average. Stable numbers make speedup claims trustworthy.
Count the Copies
Be honest about what you measure. Kernel-only time looks amazing, but real speedup must include the PCIe transfers too.
Compute the Speedup
Speedup is simply CPU time divided by GPU time. A small array may even be slower on the GPU once copies are counted.
float speedup = cpu_ms / gpu_ms;Bigger Arrays Win More
The GPU shines when there is enough work to hide transfer cost. Grow n and watch the speedup climb as parallelism dominates. 📈
Quick Check
Which tool gives accurate GPU kernel timing?
Recap
You measured your first speedup with CUDA events: warm up, bracket the kernel, sync, and divide CPU by GPU time. Bigger problems, bigger wins. 🎉
Frequently asked questions
Is the “Timing Your First Speedup” lesson free?
Yes — the full text of “Timing Your First Speedup” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Timing Your First Speedup”?
Measuring GPU vs CPU on the same task. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Timing Your First Speedup” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Vector Add Kernel
- Wiring Up the Host Side
- Verifying the Result on the CPU
- Timing Your First Speedup