Pla$orm independence and languages CSCI 136: Fundamentals of - - PowerPoint PPT Presentation

pla orm independence and languages
SMART_READER_LITE
LIVE PREVIEW

Pla$orm independence and languages CSCI 136: Fundamentals of - - PowerPoint PPT Presentation

Pla$orm independence and languages CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Language types: Compiled e.g. C++


slide-1
SLIDE 1

Pla$orm ¡independence ¡and ¡languages ¡

CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡

slide-2
SLIDE 2

Overview ¡

2 ¡

  • Language ¡types: ¡

– Compiled ¡

  • e.g. ¡C++ ¡

– Intermediate ¡/ ¡bytecode ¡ ¡

  • e.g. ¡Java, ¡.NET ¡

– Interpreted ¡

  • e.g. ¡PHP, ¡JavaScript ¡
  • Performance ¡comparison ¡
slide-3
SLIDE 3

GeneraIng ¡primes ¡

  • Problem: ¡Compute ¡all ¡the ¡primes ¡up ¡to ¡N ¡

– Prime ¡# ¡is ¡only ¡divisible ¡by ¡1 ¡and ¡itself ¡

  • ANempt ¡1: ¡Brute ¡force ¡

– Loop ¡over ¡all ¡the ¡numbers ¡[2, ¡N] ¡ – For ¡each ¡#, ¡check ¡if ¡anything ¡evenly ¡divides ¡it ¡ ¡

... ¡ for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ { ¡ ¡ ¡ ¡int ¡j ¡= ¡2; ¡ ¡ ¡ ¡while ¡((j ¡< ¡i) ¡&& ¡(i ¡% ¡j ¡!= ¡0)) ¡ ¡ ¡ ¡ ¡ ¡ ¡j++; ¡ ¡ ¡ ¡if ¡(j ¡== ¡i) ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ } ¡ ... ¡

PrimesNested.java ¡

slide-4
SLIDE 4

GeneraIng ¡primes ¡

  • ANempt ¡2: ¡Sieve ¡of ¡Eratosthenes ¡

¡

slide-5
SLIDE 5

5 ¡

public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡

Primes.java ¡

slide-6
SLIDE 6

6 ¡

¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡

Primes.java ¡

#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡

¡

int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡const ¡int ¡N ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡ ¡bool* ¡a ¡= ¡new ¡bool[N ¡+ ¡1]; ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡sqrt((double) ¡N); ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡n) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡printf("%d ¡", ¡i); ¡ ¡ ¡ ¡printf("\n"); ¡ ¡ ¡ ¡ ¡delete ¡[] ¡a; ¡ ¡ ¡ ¡return ¡0; ¡ } ¡

Primes.cpp ¡ Maybe ¡you've ¡ never ¡seen ¡C ¡ before, ¡but ¡you ¡ can ¡read ¡it! ¡

slide-7
SLIDE 7

NaIve ¡compilaIon ¡

7 ¡

hNp://xkcd.com/303/ ¡

  • C/C++ ¡

– Write ¡the ¡code ¡ – Press ¡compile ¡buNon ¡ – Wait ¡ – Fix ¡compile ¡errors ¡ – Press ¡compile ¡buNon ¡ – … ¡ – Compiled ¡naIve ¡code ¡loaded ¡directly ¡on ¡CPU ¡ – Code ¡is ¡specific ¡to ¡targeted ¡architecture: ¡

  • Compile ¡for ¡Windows ¡x86 ¡
  • Compile ¡for ¡Mac ¡x86 ¡
  • … ¡
slide-8
SLIDE 8

Source ¡code: ¡

Plain ¡text ¡file ¡created ¡in ¡a ¡high-­‑ level ¡programming ¡language ¡

#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡

¡

int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡int ¡n ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡... ¡

Primes.cpp ¡ Primes.exe ¡

Compile ¡with ¡GNU ¡C++ ¡compiler: ¡

c:\source\c\Primes>g++ -O3 Primes.cpp -o Primes

Machine ¡language: ¡

Actual ¡binary ¡run ¡by ¡a ¡ parIcular ¡processor, ¡not ¡ human ¡readable/writeable ¡ c:\source\c\Primes>Primes ¡100 ¡ 2 ¡3 ¡5 ¡7 ¡11 ¡13 ¡17 ¡19 ¡23 ¡29 ¡31 ¡37 ¡41 ¡43 ¡47 ¡ 53 ¡59 ¡61 ¡67 ¡71 ¡73 ¡79 ¡83 ¡89 ¡97 ¡ Specific ¡to ¡the ¡ architecture ¡targeted ¡ by ¡compiler ¡(e.g. ¡x86) ¡

8 ¡

slide-9
SLIDE 9

Source ¡code: ¡

Plain ¡text ¡file ¡created ¡in ¡a ¡high-­‑ level ¡programming ¡language ¡

#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ #include ¡<math.h> ¡

¡

int ¡main(int ¡argc, ¡char** ¡argv) ¡ { ¡ ¡ ¡ ¡int ¡n ¡= ¡atoi(argv[1]); ¡ ¡ ¡ ¡... ¡

Primes.cpp ¡

Compile ¡with ¡Visual ¡Studio: ¡

Primes.exe ¡ Somewhat ¡different ¡binary ¡ than ¡GNU's ¡output, ¡but ¡ should ¡work ¡on ¡same ¡

  • architecture. ¡

9 ¡

slide-10
SLIDE 10

Different ¡targets ¡in ¡Visual ¡Studio ¡

10 ¡

slide-11
SLIDE 11

Assembly ¡language ¡ C ¡ Hex ¡file ¡

8051 ¡educaFonal ¡board ¡ MRT: ¡ Loads ¡machine ¡code ¡ into ¡code ¡memory ¡of ¡ 8051 ¡board ¡ Keil ¡IDE ¡ Keil ¡IDE ¡ Primes.c ¡ Primes.asm ¡

11 ¡

slide-12
SLIDE 12

8051 ¡assembly: ¡IniIalizing ¡an ¡array ¡

12 ¡

; Parameters to our ArrayInit subroutine ArrayNum EQU 30h ; Where our array starts ArrayMemStart EQU 31h ; 1st memory location in the array ArrayInitVal EQU 32h ; What value to load into array Start: MOV ArrayNum, #10 MOV ArrayMemStart, #40h MOV ArrayInitVal, #0ABh CALL ArrayInit JMP Start ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Init the memory in an array to a value, uses R0 and R1 ArrayInit: MOV R0, ArrayNum MOV R1, ArrayMemStart ArrayInitLoop: MOV @R1, ArrayInitVal INC R1 DJNZ R0, ArrayInitLoop RET

slide-13
SLIDE 13

Source ¡code: ¡

Plain ¡text ¡file ¡created ¡in ¡some ¡ editor ¡(notepad, ¡vi, ¡TextEdit, ¡ Eclipse, ¡DrJava, ¡…) ¡

public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡n ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡... ¡

Primes.java ¡

☼LineNumberTable☺ ♦main☺ ▬([Ljava/lang/String;)V☺ StackMapTable #☺ SourceFile☺ ♂Primes.java♀ ♂ ♀ *♀ + ,☺ ♥%d ☺ ►java/lang/Object♀

Primes.class ¡

Java ¡bytecode: ¡

Intermediate ¡language ¡that ¡any ¡ device ¡running ¡Java ¡can ¡ understand ¡ % javac Primes.java c:\source\c\Primes>java ¡Primes ¡100 ¡ 2 ¡3 ¡5 ¡7 ¡11 ¡13 ¡17 ¡19 ¡23 ¡29 ¡31 ¡37 ¡41 ¡43 ¡47 ¡ 53 ¡59 ¡61 ¡67 ¡71 ¡73 ¡79 ¡83 ¡89 ¡97 ¡

This ¡guy ¡will ¡run ¡on ¡ anything ¡with ¡a ¡Java ¡ Virtual ¡Machine ¡(JVM) ¡

13 ¡

slide-14
SLIDE 14

14 ¡

Source ¡code ¡ ¡ (text) ¡ PlaTorm ¡independent ¡bytecode ¡ (binary) ¡ PlaTorm ¡dependent ¡virtual ¡ machine ¡ ¡ (binary ¡executable) ¡ Target ¡hardware ¡

slide-15
SLIDE 15

Other ¡intermediate ¡pla$orms ¡

  • Microsok ¡.NET ¡framework ¡

– Write ¡source ¡in ¡a ¡variety ¡of ¡languages: ¡

  • C#, ¡VB.NET, ¡J#, ¡F# ¡

– Compile ¡to ¡Common ¡Intermediate ¡Language ¡(CIL) ¡ – Executed ¡by ¡Common ¡Language ¡RunIme ¡(CLR) ¡

15 ¡

slide-16
SLIDE 16

16 ¡

slide-17
SLIDE 17

¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡ using ¡System; ¡ ¡ namespace ¡Primes ¡ { ¡ ¡ ¡ ¡class ¡Primes ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡static ¡void ¡Main(string[] ¡args) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡n ¡= ¡Int32.Parse(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡bool[] ¡a ¡= ¡new ¡bool[n ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡n; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.Sqrt((double) ¡n); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡n) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡n; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.Console.Write(i ¡+ ¡" ¡"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.Console.WriteLine(); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡

Primes.cs ¡ Primes.java ¡

17 ¡

slide-18
SLIDE 18

Bytecode ¡advantages ¡

  • Pla$orm ¡independence ¡

– Write ¡once, ¡run ¡anywhere ¡

  • At ¡anywhere ¡someone ¡has ¡wriNen ¡a ¡virtual ¡machine ¡
  • Possibly ¡faster ¡than ¡pre-­‑compiling??? ¡

– Just ¡In ¡Time ¡(JIT) ¡compiler ¡ ¡

  • Can ¡opImize ¡bytecode ¡to ¡CPU ¡it ¡is ¡running ¡on ¡
  • Difficult ¡to ¡do ¡this ¡if ¡you ¡compile ¡in ¡advance ¡

– You ¡would ¡have ¡to ¡deploy ¡a ¡version ¡for ¡every ¡CPU ¡variant ¡

  • Security ¡

– Virtual ¡Machine ¡(VM) ¡can ¡verify ¡code ¡is ¡behaving ¡

18 ¡

slide-19
SLIDE 19

Interpreted ¡languages ¡

  • No ¡compilaIon ¡step ¡before ¡running ¡
  • Each ¡line ¡compiled ¡as ¡encountered ¡

– Originally ¡some ¡programming ¡languages ¡ ¡

  • e.g. ¡Smalltalk, ¡BASIC ¡

– Other ¡scripIng ¡languages ¡

  • e.g. ¡DOS ¡batch ¡files, ¡bash ¡scripts, ¡PHP, ¡JavaScript ¡

– Now ¡most ¡uIlize ¡Just ¡In ¡Time ¡(JIT) ¡compilers ¡ ¡

  • Cache ¡previously ¡compiled ¡lines ¡of ¡code ¡

– Many ¡languages ¡thought ¡of ¡as ¡"interpreted" ¡now ¡ use ¡bytecode ¡approach ¡

  • e.g. ¡Python, ¡Perl, ¡Ruby ¡

19 ¡

slide-20
SLIDE 20

¡ public ¡class ¡Primes ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡final ¡int ¡N ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡[] ¡a ¡= ¡new ¡boolean[N ¡+ ¡1]; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[i] ¡= ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡top ¡= ¡(int) ¡Math.sqrt((double) ¡N); ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡top; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡j ¡= ¡2 ¡* ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(j ¡<= ¡N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡+= ¡i; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡2; ¡i ¡<= ¡N; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[i]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("%d ¡", ¡i); ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.printf("\n"); ¡ ¡ ¡ ¡} ¡ } ¡

Primes.java ¡ Primes.php ¡

<?php ¡ ¡ ¡ ¡ ¡ $n ¡= ¡$_GET["N"]; ¡ ¡ $a ¡= ¡array(); ¡ for ¡($i ¡= ¡0; ¡$i ¡<= ¡$n; ¡$i++) ¡ ¡ ¡ ¡ ¡$a[$i] ¡= ¡true; ¡ ¡ $top ¡= ¡intval(sqrt($n)); ¡ for ¡($i ¡= ¡2; ¡$i ¡<= ¡$top; ¡$i++) ¡ { ¡ ¡ ¡ ¡ ¡$j ¡= ¡2 ¡* ¡$i; ¡ ¡ ¡ ¡ ¡while ¡($j ¡<= ¡$n) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$a[$j] ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$j ¡+= ¡$i; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡($i ¡= ¡2; ¡$i ¡<= ¡$n; ¡$i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡($a[$i]) ¡echo ¡"$i ¡"; ¡ ¡ ¡ ¡ ¡echo ¡"<br ¡/>"; ¡ ¡ ¡ ¡ ¡ } ¡ ¡ ?> ¡

20 ¡

slide-21
SLIDE 21

Benchmarks ¡

  • Find ¡the ¡primes ¡up ¡to ¡123,456,789 ¡

– Using ¡Sieve ¡of ¡Eratosthenes ¡ ¡ – Repeated ¡ten ¡Imes ¡ – Disabled ¡console ¡output ¡for ¡Iming ¡

21 ¡

Language ¡ Compiler ¡ Type ¡ Tower ¡ MacBook ¡ Pro ¡ ThinkPad ¡ x41 ¡ C++ ¡ Visual ¡Studio ¡2010 ¡ compiled ¡ 164.9 ¡

  • ­‑ ¡

834.3 ¡ C++ ¡ GNU ¡g++ ¡ compiled ¡ 161.0 ¡ 75.0 ¡ 830.7 ¡ Java ¡ javac ¡1.6.0_26 ¡ bytecode ¡ 158.2 ¡ 75.2 ¡ 843.0 ¡ C# ¡ Visual ¡Studio ¡2010 ¡ ¡ bytecode ¡ 162.6 ¡ 76.4 ¡ 819.6 ¡ C# ¡ Mono ¡ bytecode ¡ 162.5 ¡ 76.8 ¡ 795.1 ¡ PHP ¡ (only ¡to ¡12,345,678) ¡ 1 ¡GB ¡memory ¡ interpreted ¡ 300.9 ¡ 258.1 ¡ 568.8 ¡

slide-22
SLIDE 22

Summary ¡

  • Deploying ¡your ¡program ¡

– Compiled ¡to ¡naIve ¡code ¡ – Compiled ¡to ¡byte ¡code ¡

  • Before ¡you ¡deploy ¡
  • Just-­‑in-­‑Ime ¡based ¡on ¡source ¡

– Interpreted ¡

  • Benchmark ¡results ¡

– NaIve ¡and ¡intermediate ¡byte ¡code, ¡similar ¡ – Interpreted ¡much ¡slower ¡

22 ¡