Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Slides on Compiler Organization | CMSC 212, Study notes of Computer Science

Material Type: Notes; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-okj
koofers-user-okj ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download Lecture Slides on Compiler Organization | CMSC 212 and more Study notes Computer Science in PDF only on Docsity! 1 1 1CMSC 212 โ€“ S06 (lect 25) Announcements  Program #6 โ€“ is due in a week from Thursday  Reading โ€“ Notes 2CMSC 212 โ€“ S06 (lect 25) Improving Code Performance  This lecture is not about algorithms, but rather โ€“ how to convert algorithms into efficient code โ€“ how to refine code to make it run faster  Helpful to know a bit about โ€“ what a compiler can and can't do โ€“ what takes time on this specific hardware  Key Ideas โ€“ Be systematic and data driven in what you do โ€ข Easy to get into making random code changes โ€“ Programs spend their time in โ€ข loops (or recursion) โ€“ a sequence of code executed once is fast โ€ข doing I/O 2 2 3CMSC 212 โ€“ S06 (lect 25) Know Your Compiler  Compilers can be asked to "optimize" your code โ€“ -O flag (and -On) enables the optimizer โ€“ they are supposed to never break your code โ€ข only safe changes to the program are permitted  Limits on compiler optimizations โ€“ should never alter correct programs โ€ข may discover latent bugs though โ€“ limited understanding of the program โ€ข you are much smarter than the best compiler โ€“ need to compile program quickly 4CMSC 212 โ€“ S06 (lect 25) Understanding Modern Processors  Pipelined โ€“ parts of multiple instructions running at once โ€“ decode instruction, load from memory  Superscalar processors โ€“ Can execute 2 or more instructions at once  Branch prediction โ€“ Computer guess which way a branch will go โ€“ Allows pipeline to stay full  Cache memory โ€“ keep copies of recently accessed memory in fast storage โ€ข recently accessed memory is much faster  Floating point takes much longer than integer โ€“ typically 20-30x for the same operation 5 5 9CMSC 212 โ€“ S06 (lect 25) Common Code Changes - Use Pointers  Recall int limit = strlen(data); for (i=0; i < limit; i++) { if (data[i] > 'A' && data[i] < 'Z') data[i] -= ('A' - 'a'); }  With a function call int limit = strlen(data); for (i=0; i < limit; i++) { makelower(&data[i]); }  Can eliminate strlen call for (ptr = data; *ptr; ++ptr) { if (*ptr > 'A' && *ptr < 'Z') *ptr -= ('A' - 'a'); }  Can move one subtraction outside int diff = โ€˜Aโ€™ โ€“โ€™aโ€™; for (ptr = data; *ptr; ++ptr) { if (*ptr > 'A' && *ptr < 'Z') *ptr -= diff; } 10CMSC 212 โ€“ S06 (lect 25) Common Code Transformation - Don't write small functions  Functions calls take time โ€“ Recall last week's code to setup calls stacks  1-2 line functions โ€“ take more time for the call than work โ€“ provide relatively little isolation of ideas  Solution: โ€“ Define abstractions so work is larger than a few lines 6 6 11CMSC 212 โ€“ S06 (lect 25) Approach To Applying These Techniques  For a big program โ€“ Hard to know where to start to optimize  Start with gprof data โ€“ concentrate in parts of the program that take the most time  Ahmdals Law โ€“ if tuning a function that takes ฮฑ share of the time โ€“ and make it k times faster โ€“ Tnew= ( 1 - ฮฑ) Told + (ฮฑTold)/k โ€“ Basically are limited by how big ฮฑ is
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved