Showing posts with label programming tips. Show all posts
Showing posts with label programming tips. Show all posts

Thursday, January 3, 2008

A way to reduce you code's computation time that might be forgotten in programming



Considering this simple example of programming code 

A=XY+XZ ===> this coding requires 3 computational steps (2 multiplications and 1 addition)

alternatively,

A=X(Y+Z) ===> this coding requires only 2 computational steps (1 addition and 1 multiplication)

บางครั้งการเปลี่ยนแปลงโค๊ดเล็กๆน้อยๆก็ช่วยขั้นตอนในการคำนวณได้
ตัวอย่างข้างบน อันแรกคอมฯต้องคำนวณ 3 ที (คูณสองทีบวกที่นึง)
ส่วนอันที่สอง คำนวณแค่ 2 ที (บวกทีกับคูณที)
ลดขั้นตอนไปได้ 1/3
เรื่องง่ายๆแต่ว่าบางทีเราก็ละเลยไป ถ้าจุดประสงค์คือต้องการลดเวลาในการคำนวณล่ะก็ต้องไม่ลืมข้อนี้