เครื่องมือจับเวลาและทดสอบประสิทธิภาพ
วัดผลก่อนปรับปรุงประสิทธิภาพ
เครื่องมือจับเวลาและทดสอบประสิทธิภาพ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Measure Before You Optimize
Never guess where time goes. You first measure your code, because the slow part is rarely the part you expected. ⏱️
The time Module
Mojo ships a time module with helpers to read the clock. It is your simplest tool for timing a block of work.
from time import nowReading the Clock
The now function returns the current time in nanoseconds. Capture it just before and just after the work you want to measure.
var start = now()
# ...work...
var end = now()Computing Elapsed Time
Subtract start from end to get the elapsed nanoseconds. Divide to convert into milliseconds or seconds for readable output.
var elapsed_ns = end - start
var ms = elapsed_ns / 1_000_000Wall Clock vs Work
Wrap only the code you care about. If you also time setup or printing, that noise hides the real compute cost you are chasing.
Use the benchmark Module
For serious numbers, Mojo offers a benchmark module that runs your code many times and reports a stable average automatically.
from benchmark import keepKeep Results Alive
The keep function tells the compiler a value really matters. Without it, dead-code removal may delete the work you meant to time.
var r = compute()
keep(r)Repeat for Stable Numbers
One run is noisy from caches and the OS. Running many iterations and averaging gives a number you can actually trust.
Warm Up the Code
The first run is often slow due to cold caches and startup. A short warmup you discard makes later timings reflect steady-state speed.
Pick a Meaningful Input
Tiny inputs finish too fast to measure well. Choose an input big enough that the work takes a steady, measurable amount of time.
Time Consistently
Always benchmark on the same machine with little running in the background. Stable conditions keep one run comparable to the next.
Quick Check
Think about why we time code at all.
Recap
You learned to measure first: read now from the time module, use benchmark with keep, warm up, repeat, and time steady work. 🎯
คำถามที่พบบ่อย
บทเรียน “เครื่องมือจับเวลาและทดสอบประสิทธิภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เครื่องมือจับเวลาและทดสอบประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เครื่องมือจับเวลาและทดสอบประสิทธิภาพ”
วัดผลก่อนปรับปรุงประสิทธิภาพ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เครื่องมือจับเวลาและทดสอบประสิทธิภาพ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เครื่องมือจับเวลาและทดสอบประสิทธิภาพ
- การค้นหาคอขวดที่แท้จริง
- การปรับปรุงสิ่งที่สำคัญ
- การอ่านรายงานการวิเคราะห์ประสิทธิภาพ