# Lecture 24: Critical Thinking. Code Optimization Aspects.

## Lecture Summary

* Last time
  * Parallel computing, multi-core: how caches come into play
  * Critical thinking, and similar tricks for speeding up your code
* Today
  * Critical thinking, and other similar tricks for speeding up your code

## Know your hardware

![Know your bandwidth/latency](https://1313833672-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTslgmrrtRXvxD2lk9%2F-MXzBURB_Y36WAVprUxe%2F-MXzDYa0tV1dJtz9aCO7%2FScreen%20Shot%202021-04-10%20at%2011.39.24%20PM.png?alt=media\&token=8a4351c0-95c8-4063-b068-ad5293a29913)

## Choose the right algorithm

* When working on a problem, there's more to performance than asymptotic complexity
  * Because asymptotic complexity is most often defined by the number of operations
  * Memory transactions are rarely considered: they are specific to the hardware
* Assess the arithmetic intensity associated with your problem

![](https://1313833672-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTslgmrrtRXvxD2lk9%2F-MXzBURB_Y36WAVprUxe%2F-MXzESdkvKR6BfxCfv61%2FScreen%20Shot%202021-04-10%20at%2011.43.21%20PM.png?alt=media\&token=1c2b789a-729d-46db-b572-a2c4aee28121)

* Simple optimization: Fusing transformations and do not bring data into the cache twice

## Compiler

* Aggressive optimizations done by the compiler might change the behavior of your code
* To help the compiler:
  * Allow it to see as much code as possible
  * Provide flags to convey information (e.g., the target architecture)
* There are a lot of amazing things covered in this lecture. The takeaways are:
  * Compilers are fantastic
  * Know them better to use them wisely
* A quick example is down below. Refer to the [slides](https://uwmadison.app.box.com/s/kapdp4qt18c6869dnaenlo35iytnlp2r) for a lot more fun facts

![](https://1313833672-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTslgmrrtRXvxD2lk9%2F-MXzBURB_Y36WAVprUxe%2F-MXzI3IUdr6jxw-hCJ01%2FScreen%20Shot%202021-04-10%20at%2011.59.06%20PM.png?alt=media\&token=fe840cd0-3d62-45fb-ba60-b7b5042e55ee)

![](https://1313833672-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MMTslgmrrtRXvxD2lk9%2F-MXzBURB_Y36WAVprUxe%2F-MXzI64jS_qzpu4BFr0-%2FScreen%20Shot%202021-04-10%20at%2011.59.17%20PM.png?alt=media\&token=dc487d67-f0ff-4975-af36-314605bf80f8)
