# Astro 585 discussion 4

10 February 2014 astro585

## Basic optimization techniques for serial code

##### Ideas for simple optimizations
• Don't call your functions too often

• Pass pointers rather than by reference

• Know what your computer is doing - what is going on under the hood?

• Providing types at compile-time

• If using an interpreted language: Vectorize!

• Reduce branching

##### Loops

Compilers can be smart, and can speed up one-dimensional loops. They have a harder time to figure out of how to make nested loops faster.

## Difference between unit tests and regression tests

##### Unit tests

A test that makes sure that one function in your code (or one small, and very well defined part of your code) works properly.

##### Regression tests

Bigger scale tests, that usually combine many functions, and to check if they are working correctly together. For example: Calculating the Planck spectrum and then write the results to an external ASCII file.

## Using _Try-Catch

Its a way to see if the program says: "Hey! There is a problem here!" - then if you don't have a Try-Catch clause your program will crash. An IF statement can also work if you know how it can fail. However, the advantage of Try-Catch is that you can catch some errors that are buried deep down maybe. Still, its a much more expensive way to deal with a problem, rather than using an IF statement. Its for things that don't have a clear order. It doesn't really happen too often in scientific programming. Good places to use would be in I/O, and networks.

## Q. Should we optimize serial code first, before we parallelize it?

It kind of depends on your problem. They key is to think about: "How are you gonna parallelize it?" It can be embarrassingly parallel , and you can then basically distribute the serial code to a lot of workers and gain speedups that way. If not, then you can also go into the idea of parallelizing at multiple levels.

#### Contact Info

Guðmundur Kári Stefánsson

| 500px | vimeo | facebook |

email: gws5257 [at] psu.edu

#### Recent posts!

09 March 2015 Installing a CDK24 Telescope at Penn State

06 December 2014 A Day in Pittsburgh

12 November 2014 HET trip - Results

11 November 2014 HET trip - day 1

01 October 2014 Black Moshannon State Park Observing

29 September 2014 HPF MLI blanket fabrication

19 September 2014 MLI Blankets

11 August 2014 HPF subsystem assembly

21 July 2014 Astrofest 2014

13 June 2014 HPF - Keeping it cool

# Tags

Ergonomically handwritten code, with a little help from my friends; vim, jekyll and twitter bootstrap