Compiler optimizations matter. One time in helping a fellow Ph.D. student improve a simulator's performance, I did two things: first, I replaced an expensive data structure with a more efficient one. Second, I turned on compiler optimizations. Together, the simulator ran 100x faster.
A question posted in the stackexchange system asked, "Why are there so few C compilers?" The main answer pointed out that any C compiler needs to be optimizing. Lots of optimizations are occurring on every compilation, and each one gaining tiniest increments in performance. While I enjoy discussing them in detail, I generally wave my hands and tell of how they are good, yet make debugging difficult. These optimizations are lumped together as the "optimization level".
In "What Every Programmer Should Know About Compiler Optimizations", we revisit optimizations. First, the compiler is no panacea and cannot correct for inefficient algorithms or poor data structure choices (although I am party to research on the later). The article then suggests four points to assist the compiler in its efforts at optimizing the code.
"Write understandable, maintainable code." Please do this! Usually, the expensive resource is the programmer. So the first optimization step is to improve the programmer's efficiency with the source code. Remember Review: Performance Anti-patterns and do not start optimizing the code until you know what is slow.
"Use compiler directives." Scary. Excepting the inline directive, I have used these less than a half dozen times in almost as many years of performance work. Furthermore, the example of changing the calling convention is less relevant in 64-bit space where most conventions have been made irrelevant.
"Use compiler-intrinsic functions." (see Compiler Intrinsics the Secret Sauce) This can often dovetail with the first point by removing ugly bit twiddling code and putting in clean function calls.
"Use profile-guided optimization (PGO)." This optimization is based on the dynamic behavior of the program. Meaning that if you take a profile of the program doing X, and later the program does Y; executing Y can be slower. The key is picking good, representative examples of the program's execution.
So you have dialed up the optimization level, and written understandable code sprinkled with intrinsics. Now what? The next step (with which I agree) is to use link time optimizations (LTO) / Link-Time Code Generation (LTCG). This flag delays many optimizations until the entire program is available to be linked. One of the principles of software performance is that the more of the program available to be optimized, the better it can be optimized. (This principle also applies in computer architecture). Thus, by delaying many optimization until the entire program is available, the linker can find additional and better opportunities than were present in individual components.
The article notes, "The only reason not to use LTCG is when you want to distribute the resulting object and library files." And alas, I have fought several battles to overcome this point, as my work requires the use of LTO. Perhaps in the next decade, LTO will be standard.
A discussion of how to do Computer Science well, particularly writing code and architecting program solutions.
Showing posts with label article. Show all posts
Showing posts with label article. Show all posts
Monday, February 23, 2015
Monday, January 28, 2013
Repost: When Beauty is Not Truth
While it was not a field discussed in the article, When Beauty Is Not Truth, nonetheless I wonder about the focus I put on elegance in programming (starting with the name of the blog). So let's consider a couple of quick things about Computer Science and the beauty of the code. I should add that the article discusses a rough equivalence between beauty, elegance, and simplicity.
First, beautiful code is not always correct.
Second, beautiful code, by virtue of its simplicity, is less likely to have bugs.
Third, beautiful code is more readable, which facilitates comprehension.
(Now back to preparing the lecture for today's class)
First, beautiful code is not always correct.
Second, beautiful code, by virtue of its simplicity, is less likely to have bugs.
Third, beautiful code is more readable, which facilitates comprehension.
(Now back to preparing the lecture for today's class)
Thursday, May 10, 2012
The Minimum of Computer Science
The NY Times ran an article at the start of last month on what CS education is required for non-majors, Computer Science for Non-Majors. Is the knowledge of how to write a (simple) program so vital to modern work that it should rank with multiplication tables, English grammar, etc? And if so, how much knowledge and in what language(s) should this be taught?
Should someone learn the basics of javascript, so they can add to webpages? Perhaps a little python to do simple processing of data? But probably not C, as the language expresses what the computer is to do and not what the computer should do.
Edit: A strong response to this general question here.
Should someone learn the basics of javascript, so they can add to webpages? Perhaps a little python to do simple processing of data? But probably not C, as the language expresses what the computer is to do and not what the computer should do.
Edit: A strong response to this general question here.
Friday, October 1, 2010
Is elegance pretty?
Through the course of my work and reading, I encountered a write-up about what makes code "pretty" with reasonable suggestions for establishing and maintaining a code style. The essential objective is to make the code readable by others. As I work with undergrads, they are not always thrilled about the effort required for "pretty" code, yet they can accept that I have to read and understand their program. Yet this barely compares to the effort required in a commercial context, where the code may have a multi-year lifetime and pass through many programmers.
Is pretty code elegant? No, although the reverse is true (and thus the title). Elegant code has an aesthetic aspect that can be termed pretty, yet it must also be more than aesthetic. My continuing proposal of elegant is code that must also be efficient, extensible, reliable, etc. Basically, to be elegant, code must leave the objectors behind. Instead, you want to show others your elegant code.
So go and write pretty code.
Is pretty code elegant? No, although the reverse is true (and thus the title). Elegant code has an aesthetic aspect that can be termed pretty, yet it must also be more than aesthetic. My continuing proposal of elegant is code that must also be efficient, extensible, reliable, etc. Basically, to be elegant, code must leave the objectors behind. Instead, you want to show others your elegant code.
So go and write pretty code.
Subscribe to:
Posts (Atom)