Programming Tips GS540 January 10, 2011.

Slides:



Advertisements
Similar presentations
Advanced microprocessor optimization Kampala August, 2007 Agner Fog
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
Discussion Section: HW1 and Programming Tips GS540.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Pointers OVERVIEW.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Introduction to Programming
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 3 Translation.
Yan Shi CS/SE2630 Lecture Notes
Object Lifetime and Pointers
Dynamic Storage Allocation
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
The Machine Model Memory
Computer Science 210 Computer Organization
C Interview Questions Prepared By:.
CSE 374 Programming Concepts & Tools
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Checking Memory Management
Introduction to Linked Lists
Pointers and References
Computer Science 210 Computer Organization
First discussion section agenda
Programming Tips GS540 January 10, 2011.
Variables Title slide variables.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Your questions from last session
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Malloc Lab CSCI 380: Operating Systems
COP 3330 Object-oriented Programming in C++
Data Structures and Algorithms Introduction to Pointers
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CSCE 206 Lab Structured Programming in C
Pointers and References
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Testing & Security Dr. X.
Procedure Linkages Standard procedure linkage Procedure has
EE 312 Exam I Review.
Dynamic Binary Translators and Instrumenters
Hello World Program In Visual Studio and Debugging
SPL – PS2 C++ Memory Handling.
Introduction to C CS 3410.
Presentation transcript:

Programming Tips GS540 January 10, 2011

Jarrett Egertson 4th year Genome Sciences Graduate Student MacCoss Lab for Biological Mass Spectrometry Email: jegertso@uw.edu Discussion Section: Thursdays 2-3 Foege S-040 Office Hours: Tuesdays: 2-3 Vista Café and by Appointment Programming: C++/C#/Python Dev environment: GCC (Linux), Visual Studio (Windows)

Outline General tips / Coding style advice Performance Debugging advice Numerical issues C Programming Pointers Sorting Homework 1

General tips Validate using toy cases with a small amount of data Figure out minimal cases by hand to verify program Print intermediate output Test important functions

Coding style advice Your audience is people as well as the computer Break large functions into small, simple functions Break large files into smaller files containing groups of related functions Use descriptive names for function arguments and variables with larger scopes (e.g. out_file, exons) Use short names for iterators, vars of limited scope, and vars that are used many times (e.g. i, j)

Performance Avoid unnecessary optimization! Better to write simple code first and improve speed if necessary Big performance gains often result from changing a few small sections of code (e.g. within loops) Move unnecessary code out of loops Avoid frequent memory allocation Allocate memory in large blocks (e.g. array of structs, not one at a time) Re-use the same piece of memory Avoid slow comparison routines when sorting Use a profiler for tough cases (gprof for C; dprofpp for perl)

Debugging advice Use assertions E.g. check probabilities: should always be >= 0 and <= 1 often should sum to 1.0 Write slow but sure code to check optimized code In difficult cases use a debugger, but avoid overuse valgrind can help find segfaults, memleaks (compile with -g first)

Numerical issues Consider using log space to avoid overflow and underflow Don’t compare floats with equals (use >= or <=, NOT ==) Beware subtracting large, close numbers Beware integer casts 1/2 is 0, but 1.0/2 is 0.5 To generate random numbers, use random() rather than rand()

Pointers in C Pointers are memory addresses (they point to other variables) The address-of operator (&) obtains the memory address of a variable The dereference operator (*) accesses the value stored at the pointed-to mem location

Pointers in C (cont’d) Arrays are pointers to blocks of memory Array indices are just pointer arithmetic and dereferencing combined: a[12] is the same as *(a+12) &a[3] is the same as a+3 From The C Programming Language by B. Kernighan & D. Ritchie

Homework 1 Declare large arrays on heap not stack Outside main() or as static Output XML markup directly Avoid copy-pasting results into an XML template

Pointers in C (cont’d) Large arrays should be dynamically allocated (on the heap) C C++ From The C Programming Language by B. Kernighan & D. Ritchie

Pointers in C (cont’d) Attributes of pointed-to structures can be derefenced with “arrow notation”: a->elem is equivalent to (*a).elem

Sorting (in C)

Words of wisdom "Everything should be made as simple as possible, but no simpler." -- Albert Einstein KISS principle: “Keep It Simple, Stupid” From The Zen of Python by Tim Peters: Beautiful is better than ugly Explicit is better than implicit Simple is better than complex Complex is better than complicated Flat is better than nested Sparse is better than dense Readability counts