ThinkingLowWritingHighLevel_RandallHyde Flashcards
What should we know before reading this book?
Competent in one procedural language (C, C++, BASIC, Pascal, Assembly, Ada, FORTRAN, Modula-2)
Able to design a software solution for a small problem description
Basic grasp of machine organization and data representation
Hexadecimal and binary numbering systems
How computers represent high level data types like signed integers, characters, strings in memory.
Why?
We’ll receive the greatest benefit from the material if we have these skills
What topics does this volume cover?
Why it’s important to consider low-level execution of your high level programs
How compilers generate machine code from high-level language (HLL) statements
How compilers represent various data types using low-level, primitive, data types
How to write HLL code to help the compiler produce better machine code
How to take advantage of a compiler’s optimization facilities
How to “think” in assembly language (low-level terms) while writing HLL code
What language was high-performance software written during the early days of the personal computer revolution?
Assembly language
Then?
Optimizing compilers for HLL were improved and author’s began to claim the machine instructions generated by compilers performed at 90% of hand-optimized assembly speed
What problem happened with the new generation of programmers using high-level languages?
They did not have benefit of understanding assembly!
Therefore, they were unable to choose statements and data structures that HLLs could efficienctly translate into machine code
Original programmers did understand assembly and used HLLs with low-level ramifications in mind
What is the purpose of this book?
Teach you what you need to know to write great code without having to become an expert at assembly language
What is the major problem with using optimizing compilers?
They allow programmers to get lazy!
No optimizing compiler can make up for poorly written HLL source code.
What is a misconception among HLL programmers?
optimization algorithms in modern compilers will produce efficient code regardless of what they feed into their compilers.
No!
It’s easy to feed a compiler poorly written code that stymies the optimization algorithms of a compiler!
What is the problem with high-level language programmers?
They never actually looked at the machine code the compiler produces from their high-level language source code!
They blindly assume the compiler is doing a good job because they’ve been told:
“compilers produce code that is almost as good as what an expert assembly language programmer can produce.”
What won’t a compiler do?
Change your algorithms in order to improve the performance of your software
Ex. Use a linear search rather than binary search
cannot expect compiler to substitute better algorithm for you
It can improve speed of your linear search but improvement may be nothing compared to using a better algorithm
What type of code is comparable to code produced by expert assembly language programmers?
Manually optimized code:
High Level: Select better algorithms (independent of compiler and language)
Lower Level: Manually optmize code based on HLL (apply across different compilers, same language)
Lower Level: Compiler specific optmizations (structure code to take advantage of tricks in specific versions of compilers)
Lowest Level: Consider machine code compiler emits and adjust HLL statemnts to force desired sequence of machine instructions
The issue?
Most people do not go to these extremes to write their HLL code
What are the manual levels of optimization?
Most abstract: Select better algorithms (independent of compiler and language)
Less Abstract : Manually optmize code based on HLL (apply across different compilers, same language)
Less Abstract: Compiler specific optmizations (structure code to take advantage of tricks in specific versions of compilers)
Least Abstract: Consider machine code compiler emits and adjust HLL statemnts to force desired sequence of machine instructions
Why?
Programmers employing this process will produce the best possible machine code!
Under what conditions can an optimizing compiler produce code that is almost as good as hand-optimized assembly language?
When the HLL code is written in an appropriate fashion to achieve these performance levels
To write HLL code like this requires understanding how computers operate and execute software
How do I write high-level code so that the compiler can translate it most efficiently?
Answer:
Think in assembly, write in high-level language
What does the fact that we can achieve the same result with two high-level language code sequences not imply?
That the compiler generates the same sequence of machine instructions for each sequence
Ex. If-else vs. switch statement
in HLL achieve same result, machine instructions are different!
Which is faster?
Unless we understand how compiler translates statements like these into machine code and understand different efficiencies between machine instructions, cannot choose one over the other!
When will the compiler rarely generate the best possible machine code?
If a programmer does not consider the low-level ramifications of the HLL code (how it’s translated into machine code and the efficiencies of various machine instructions)