Compiling “premature optimization is the root of all evil.” -Donald Knuth.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Chapter 11 Introduction to Programming in C
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Chapter 7: User-Defined Functions II
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 1: Introduction
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Common Lisp! John Paxton Montana State University Summer 2003.
Introduction to a Programming Environment
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Source Code Basics. Code For a computer to execute instructions, it needs to be in binary Each instruction is given a number Known as “operation code”
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Language Systems Chapter FourModern Programming Languages 1.
Block-Structured Procedural Languages Lecture 11: Dolores Zage.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Variable Scope Storage Class Recursion
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
Debugging and Profiling With some help from Software Carpentry resources.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM John.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
11 Speed & Debug.  Lisp is really two languages:  A language for writing fast programs  A language for writing programs fast  In the early stage,
Programming Languages
Miscellaneous Topics Packages –Provide separable name spaces for functions, constants, macros –We can encapsulate code in packages like classes in Java.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Java FilesOops - Mistake Java lingoSyntax
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
The Hashemite University Computer Engineering Department
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Overview: Lecture 6: Dolores Zage. What is a program? n Operations that are to be applied to certain data in a certain sequence (definition holds for.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Programming Fundamentals Enumerations and Functions.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
A First Book of ANSI C Fourth Edition
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Software Engineering Algorithms, Compilers, & Lifecycle.
. A little bit about optimization, 2d array, 1d array used as 2d, register and volatile.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 3 Translation.
Advanced Computer Systems
Run-Time Environments Chapter 7
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
User-Written Functions
Types for Programs and Proofs
Overview of Compilation The Compiler BACK End
Chapter 5 Conclusion CIS 61.
Function There are two types of Function User Defined Function
CS 301 Fall 2001 – Chapter 3 Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel 9/17/2018.
Chapter 11 Introduction to Programming in C
Overview of Compilation The Compiler BACK End
point when a program element is bound to a characteristic or property
1.3.7 High- and low-level languages and their translators
SPL – PS1 Introduction to C++.
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Presentation transcript:

Compiling “premature optimization is the root of all evil.” -Donald Knuth

Optimization In LISP, you can write slow code fast or fast code slow Experience will help you overcome common pitfalls (over use of append in recursive algorithms for example) In general, worry about the top level algorithms first. You can always speed it up later

Compiling LISP can be compiled into machine code just like other languages – Typically results in a drastic speed up for LISP programs Compilation can reduce both running time and memory consumption

Time a Function You can see how long it takes to evaluate a function by wrapping the function call in a call to time (defun waste-time (n) (dotimes (i n) (* i i))) >(time (waste-time 50000)) real time : secs run-gbc time : secs child run time : secs gbc time : secs NIL

Compile >(time (waste-time )) real time : secs run-gbc time : secs child run time : secs gbc time : secs NIL >(compile 'waste-time) […Compiler output…] >(time (waste-time )) real time : secs run-gbc time : secs child run time : secs gbc time : secs NIL

Disassemble We can look at the assembly code produced by compile using disassemble System and implementation dependent Here’s an example in CLISP >(defun add1 (n) (1+ n)) ADD1 >(disassemble 'add1) Disassembly of function ADD1 1 required argument 0 optional arguments No rest parameter No keyword parameters 3 byte-code instructions: 0 (LOAD&PUSH 1) 1 (CALLS2 151) ; 1+ 3 (SKIP&RET 2) NIL GCL output can be a mess since it actually transforms LISP into C code

Compile-File You can also compile all functions in a given file (compile-file "file.lisp") To run functions from the compiled file, load it like any other file (load "file.fas") The specific file extension depends on implementation

Inlining You can make a function be an inline function by using declare (defun f (x) (* x x)) (defun g (x) (declare (inline f)) (+ (f x) (f x))) Now, upon compiling g, the actual body of f will be copied into g in the pertinent locations, though only within g. To make f be an inline function everywhere, use declaim instead of declare, within the global scope. Cannot be used with recursive functions

More Type Safety declare and declaim can specify other compiler directives You can use to streamline compiled functions to only expect certain types using type >(defun f (x y) (declare (type integer x y)) (+ x y)) F >(f ) 9.0 >(compile 'f) …Compiler output… >(f 3 4) 7 >(f ) Error

Optimizing The optimize declaration allows several optimization priorities to be set Priorities are on a scale from 0 to 3, with 3 the most important – compilation-speed : speed of the compilation process – debug : ease of debugging – safety : run-time error checking – space : both code size and run-time space – speed : speed of the object code Example specification (defun f (x y) (declare (optimize (safety 2) (speed 3))) (+ x y))