Lecture 13: Does this Code Make My Method Look Hot ?

29
LECTURE 13: DOES THIS CODE MAKE MY METHOD LOOK HOT? Computer Science 313 – Advanced Programming Topics

description

Computer Science 313 – Advanced Programming Topics. Lecture 13: Does this Code Make My Method Look Hot ?. The first 90% of the code accounts for the first 90% of the development time . The remaining 10% of the code accounts for the other 90% of the development time. Tom Cargill. - PowerPoint PPT Presentation

Transcript of Lecture 13: Does this Code Make My Method Look Hot ?

Page 1: Lecture 13: Does this Code Make My Method Look  Hot ?

LECTURE 13:DOES THIS CODE MAKE MY METHOD LOOK HOT?

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 13: Does this Code Make My Method Look  Hot ?

Tom Cargill

The first 90% of the code accounts

for thefirst 90% of the

development time. The

remaining 10% of the code

accounts for the other 90% of the

development time.

Page 3: Lecture 13: Does this Code Make My Method Look  Hot ?

Let’s Get Coding

Who will write next lab in assembly? Can only use assembly code for entire lab

pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) cmpl $0, -4(%ebp)

Page 4: Lecture 13: Does this Code Make My Method Look  Hot ?

Let’s Get Coding

Who will write next lab in assembly? Can only use assembly code for entire lab

During the first pass, apply standard optimizations

pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) cmpl $0, -4(%ebp)

Page 5: Lecture 13: Does this Code Make My Method Look  Hot ?

Let’s Get Coding

Who will write next lab in assembly? Can only use assembly code for entire lab

During the first pass, apply standard optimizations

Every method should be tuned to maximize branches

pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) cmpl $0, -4(%ebp)

Page 6: Lecture 13: Does this Code Make My Method Look  Hot ?

Let’s Get Coding

Who will write next lab in assembly? Can only use assembly code for entire lab

During the first pass, apply standard optimizations

Every method should be tuned to maximize branches

Necessary to achieve top performance

pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) cmpl $0, -4(%ebp)

Page 7: Lecture 13: Does this Code Make My Method Look  Hot ?

Promise of Perfect Debugging Programs just λ-calculus equations Use Java, but instead prove program

correct Translate program into λ-calculus Specify theorem & prove program

correctness

Page 8: Lecture 13: Does this Code Make My Method Look  Hot ?

Fewest Lectures Ever

What if guaranteed A for student doing this? Would still need to submit assignment

on time Working assembly needed (with

optimizations) Code should work, but proof must be

perfect

Any takers?

Page 9: Lecture 13: Does this Code Make My Method Look  Hot ?

Fewest Lectures Ever

What if guaranteed A for student doing this? Would still need to submit assignment

on time Working assembly needed (with

optimizations) Code should work, but proof must be

perfect

Any takers? Why not?

Page 10: Lecture 13: Does this Code Make My Method Look  Hot ?

Why Isn’t This Done?

Almost never use these techniques Low-level device drivers use some C (or

similar) Rare for partial proof of correctness to

be used Simple result from easy cost-benefit

analysis Techniques requires massive amounts of

time Time is money; these are very, very

expensive The provided benefits just not worth it

Page 11: Lecture 13: Does this Code Make My Method Look  Hot ?

What’s The Secret?How do I know

when to optimize? I

need to know. Please, please,

please?

Page 12: Lecture 13: Does this Code Make My Method Look  Hot ?

Problem is Universal

Frequent need to make optimal decision Using reagents during chemical

synthesis Take power plant offline & perform

maintenance Fastest torrent from which to download

Cannot optimize everything Instead we only focus efforts where it

matters

Page 13: Lecture 13: Does this Code Make My Method Look  Hot ?

Problem is Universal

Frequent need to make optimal decision Using reagents during chemical

synthesis Take power plant offline & perform

maintenance Fastest torrent from which to download

Cannot optimize everything Instead we only focus efforts where it

matters

But how can I know what is important?

Page 14: Lecture 13: Does this Code Make My Method Look  Hot ?

Are Graphics Important?

Rewrite Swing to improve its performance?

Page 15: Lecture 13: Does this Code Make My Method Look  Hot ?

Are Graphics Important?

Rewrite Swing to improve its performance? Hells, yes. We

need 60 fps for new games.

Page 16: Lecture 13: Does this Code Make My Method Look  Hot ?

Are Graphics Important?

Rewrite Swing to improve its performance? Hells, yes. We

need 60 fps for new games.

%#$ no. What does it do for search speed?

Page 17: Lecture 13: Does this Code Make My Method Look  Hot ?

Critical Paths

All that is important is code along critical path Determines time needed and not just

the fluff Graphics are not critical for Google

Major graphics improvements are nice… …but have zero affect on search times

Bungie’s critical path is graphics package Games performance limited by graphic

times Would hate GPU for search – slows them

down

Page 18: Lecture 13: Does this Code Make My Method Look  Hot ?

Guess the Critical Path

$ java -Xprof edu.canisius.ann.NetworkFlat profile of 16.4 secs (956 total ticks): main Compiled + native Method35.6% 282 + 58 Neuron.getWeightedError21.1% 187 + 15 Neuron.updateWeights13.0% 124 + 0 Network.trainNetwork 9.5% 0 + 91 java.lang.StrictMath.exp 9.5% 91 + 0 Neuron.compute 4.5% 22 + 21 Network.setNetworkInput 3.6% 34 + 0 Network.runOneInput 2.0% 0 + 19 java.util.ArrayList.<init> 0.8% 8 + 0 Network.main99.6% 748 + 204 Total compiled

Page 19: Lecture 13: Does this Code Make My Method Look  Hot ?

Still a Cost-Benefit Trade

Great. More useless geekery.

When will it make a difference?

When is it worth my time?

Page 20: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law

Determines how much faster program can go Use this to answer question that matters

Speedup calculated by this equation Speedup of 2 means program twice as

fast Need 1/10 the time, 10 is speedup factor of

approach When speedup is 1, time taken

unchanged Speedup of ½ == twice as long as past

approach

Page 21: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law

Determines how much faster program can go Use this to answer question that matters

Speedup calculated by this equation Speedup of 2 means program twice as

fast Need 1/10 the time, 10 is speedup factor of

approach When speedup is 1, time taken

unchanged Speedup of ½ == twice as long as past

approach(But usually say slowdown factor of 2)

Page 22: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law

Determines how much faster program can go Use this to answer question that matters

Speedup calculated by this equation Speedup of 2 means program twice as

fast Need 1/10 the time, 10 is speedup factor of

approach When speedup is 1, time taken

unchanged Speedup of ½ == twice as long as past

approach(But usually say slowdown factor of 2)(Or hide report that shows this sucks)

Page 23: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law Equation

%changed of time in methods to be optimized Expressed as decimal between 0 - 1

All other time from run is %unchanged Another decimal from 0 – 1

If %changed + %unchanged ≠ 1, you FAIL

Page 24: Lecture 13: Does this Code Make My Method Look  Hot ?

Applying Amdahl’s Law

Made getWeightedError twice as fast Speedupchanged = 2, since now twice as fast Took 16.4 seconds before, what will it take

now?

Get execution times using java –Xprof

35.6% 282 + 58 Neuron.getWeightedError

%changed = 0.356%unchanged= 1 - 0.356 =

0.644

Page 25: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law

Speedup = 1.21

Page 26: Lecture 13: Does this Code Make My Method Look  Hot ?

Applying Amdahl’s Law

Move Neuron.compute off critical path? Removes it from program execution time Speedupchanged = ∞

Get execution times from java –Xprof

9.5% 91 + 0 Neuron.compute

%changed = 0.095%unchanged= 1 - 0.095 =

0.905

Page 27: Lecture 13: Does this Code Make My Method Look  Hot ?

Amdahl’s Law

Speedup = 1.10

Page 28: Lecture 13: Does this Code Make My Method Look  Hot ?

20%

43%

56%

64%

69%

73%

76%

79%

81%

83%

84%

85%

0%10%20%30%40%50%60%70%80%90%

5%10%25%50%75%90%

%Faster Optimized Section Runs

Ove

rall

Redu

ctio

n In

Tim

eWhat Amdahl’s Law Means

Page 29: Lecture 13: Does this Code Make My Method Look  Hot ?

For Next Class

Lab #3 available on Angel Asks you to implement Decorator

Pattern Have time Friday, but may want help

profiling For Friday, read pages 109-122 in

book Allocation sucks; can we do anything

about it? Why would we care about improving

allocation? When would this help write better (good)

code?