Rasmus Nielsen ([email protected]) · The .NET framework library is very big. Particularly the user...

38
. . . . . . . . Python & IronPython An introduction to Python and using IronPython on the .NET Framework Rasmus Nielsen ([email protected]) IT University of Copenhagen March 1, 2010 Rasmus Nielsen ([email protected]) Python & IronPython

Transcript of Rasmus Nielsen ([email protected]) · The .NET framework library is very big. Particularly the user...

Page 1: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

.

.

Python & IronPythonAn introduction to Python and using IronPython on the .NET Framework

Rasmus Nielsen ([email protected])

IT University of Copenhagen

March 1, 2010

Rasmus Nielsen ([email protected]) Python & IronPython

Page 2: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

What is Python?

Python is named in honour of Monty PythonPython is an interpreted, dynamically typed, high-levellanguagePython combines remarkable power with very clean, clear, andspare syntaxPython is a multi-paradigm language supporting:

Object-oriented programmingProcedural programmingAspect-oriented programmingFunctional programmingMetaprogramming

Rasmus Nielsen ([email protected]) Python & IronPython

Page 3: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Similarities to Java and C♯

Typically compiled to bytecode, but compilation is implicitEverything inherits from “object” INCLUDING:

numbersfunctionsclasses. . . – everything is “first-class”

Vast, powerful standard libraryGarbage collectionIntrospection / reflectionSerializationParametric polymorphism (Generics)Threads . . .

Rasmus Nielsen ([email protected]) Python & IronPython

Page 4: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Similaries to C

The Spirit of C...1 Trust the programmer....2 Dont prevent the programmer from doing what needs to be

done....3 Keep the language small and simple....4 Provide only one way to do an operation....5 Make it fast, even if it is not guaranteed to be portable.

Rationale for International Standard Programming Languages – C

Ad 5: Python is not intrinsically fast but IronPython is!

Rasmus Nielsen ([email protected]) Python & IronPython

Page 5: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Python vs C/C++/Java/C♯

Easy to learn, easy to teachNo notion of private, public, and sealed, etc.Typing: strong, but dynamic

Names have no typeObjects have types

No "declarations" – just statementsSpare syntax, minimal ornamentation:

No { } for blocks, just indentationNo ( ) for if/while conditionsGenerally less punctuation – no “;”

Everything is “first-class”: object classes, functions, methods,modules

Rasmus Nielsen ([email protected]) Python & IronPython

Page 6: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Python extended

Look, Ma! Python can do a lot more:

Built in support for complex numbers (like Fortran!)Multiple paradigms (OOP, procedural, generic, somefunctional)Multiple inheritance (like C++)Tuples (like SML)Multiple return values (forget about ref and out)Dynamically typed (like Ruby and JavaScript)Duck typing

Rasmus Nielsen ([email protected]) Python & IronPython

Page 7: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Implementations

CPython (C, Guido van Rossum, 1991) JPython TM−→ Jython (Java, Jim Hugunin, 1997)

IronPython (C♯, Jim Hugunin, 2006) PyPy (Python(!) 2007)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 8: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languages

Jim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 9: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this

6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 10: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks pass

Project fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 11: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython

2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 12: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to Microsoft

Microsoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 13: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!

·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 14: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹

The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 15: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0

IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 16: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The story of IronPython

2003: “Fact”: the Common Language Runtime sucks regardingdynamic languagesJim Huginin – creator of Jython – sets out to prove this6 weeks passProject fails – PyStone benchmark shows IronPython is 1.7times faster than CPython2004: Jim feels obligated to continue the work and eventuallydemonstrates the first version to MicrosoftMicrosoft hires him right away!·· v1.0 (2006) ·· v2.0 (2008) ·· v2.6 (2009) ¹The Dynamic Language Runtime built for IronPython andIronRuby is injected into vNext of the CLR and C♯ 4.0IronPython vNext will be released 1-2 months after .NET 4.0(v2.6 and v3.1 language compatibility)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 17: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Why IronPython

Reasons that CPython programmers might be interested inIronPython include:

Corporate credibility (introducing new technologies can be verydifficult in some companies, if .NET is already established then youmay need no excuse to start using IronPython)No GlobalInterpreterLock – IronPython has no GIL andmulti-threaded code can use multi core processorsThe .NET framework library is very big. Particularly the userinterface library Windows Forms is very good.IronPython is easy to embed in .NET applications as a scriptinglanguageEasier to extend than CPython (C♯ is memory managed and C♯types can be used directly in IronPython with no wrapping)Silverlight! (Die, ActionScript, Die!)

Source: http://wiki.python.org/moin/IronPython!

Rasmus Nielsen ([email protected]) Python & IronPython

Page 18: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The problem with object-oriented languages

“The problem with object-oriented languages is they’vegot all this implicit environment that they carry aroundwith them. You wanted a banana but what you got was agorilla holding the banana and the entire jungle.”

Joe Armstrong, principal inventor of Erlang

Rasmus Nielsen ([email protected]) Python & IronPython

Page 19: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The world’s smallest C♯ program – sort of. . .

Listing 1: Hello, World! in C♯1 u s i n g System ;2 u s i n g System . Collections . Generic ;3 u s i n g System . Linq ;4 u s i n g System . Text ;56 namespace ConsoleApplication17 {8 c l a s s Program9 {

10 s t a t i c vo i d Main ( s t r i n g [ ] args )11 {12 Console . WriteLine ( " He l l o , World ! " ) ;13 }14 }15 }

Rasmus Nielsen ([email protected]) Python & IronPython

Page 20: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The world’s smallest C♯ program – seriously!

Listing 2: Stripped down Hello, World! in C♯1 c l a s s Program2 {3 s t a t i c vo i d Main ( )4 {5 System . Console . WriteLine ( " He l l o , World ! " ) ;6 }7 }

Rasmus Nielsen ([email protected]) Python & IronPython

Page 21: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

The Python equivalent

Listing 3: Hello, World! in Python1 p r i n t " He l l o , World ! "

Rasmus Nielsen ([email protected]) Python & IronPython

Page 22: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Duck typing

“When I see a bird that walks like a duck and swims like aduck and quacks like a duck, I call that bird a duck.”

James Whitcomb Riley (1849-1916)

“In other words, don’t check whether it IS-a duck: checkwhether it QUACKS-like-a duck, WALKS-like-a duck, etc,etc, depending on exactly what subset of duck-likebehaviour you need to play your language-games with.”

Alex Martelli, 2000 (Member of the Python Software Foundation,“Über Tech Lead” at Google, and author of “Python in a Nutshell”)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 23: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Duck typing in Python

Listing 4: Duck typing in Python1 c l a s s Duck :2 de f quack ( self ) :3 p r i n t "Quaaaaaack ! "4 de f feathers ( self ) :5 p r i n t "The duck has wh i t e and gray f e a t h e r s . "67 c l a s s Person :8 de f quack ( self ) :9 p r i n t "The pe r son im i t a t e s a duck . "

10 de f feathers ( self ) :11 p r i n t "The pe r son t ak e s a f e a t h e r from the ground and shows i t . "1213 de f in_the_forest ( duck ) :14 duck . quack ( )15 duck . feathers ( )1617 de f game ( ) :18 donald = Duck ( )19 john = Person ( )20 in_the_forest ( donald )21 in_the_forest ( john )2223 game ( )

Rasmus Nielsen ([email protected]) Python & IronPython

Page 24: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Duck typing in action

Duck typing thus allows for polymorphism without inheritance.Duck typing makes TDD a bliss - Mock objects can be done ina jiffy.Duck typing can be implemented in C♯ 4.0 using thedynamic keyword.

Rasmus Nielsen ([email protected]) Python & IronPython

Page 25: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Test-Driven Development in Python

Listing 5: TDD in Python1 impor t unittest23 # Here ’ s our " u n i t " .4 de f IsOdd (n ) :5 r e t u r n n % 2 == 167 # Here ’ s our " u n i t t e s t s " .8 c l a s s IsOddTests (unittest . TestCase ) :9 de f testOne (self ) :

10 self . failUnless (IsOdd (1 ) )1112 de f testTwo (self ) :13 self . failIf (IsOdd (2 ) )1415 de f main ( ) :16 unittest . main ( )1718 i f __name__ == ’__main__ ’ :19 main ( )

Rasmus Nielsen ([email protected]) Python & IronPython

Page 26: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming

Aspect-oriented programming (AOP) is a programmingparadigm in which secondary or supporting functions areisolated from the main program’s business logic. It aimsto increase modularity by allowing the separation ofcross-cutting concerns, forming a basis for aspect-orientedsoftware development.

http://en.wikipedia.org/wiki/Aspect-oriented_programming

The following example is adopted from

“Pumping Iron – The State of Dynamic Languages on the .NET Framework”

by Harry Pierson (Program manager for IronPython at Microsoft))

Rasmus Nielsen ([email protected]) Python & IronPython

Page 27: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 1

Listing 6: AOP in Python part 11 p r i n t " s t a r t i n g load_logs "2 sw = Stopwatch . StartNew ( ) # System . D i a g n o s t i c s . Stopwatch3 game_logs = load_logs (file_list )4 sw . Stop ( )5 p r i n t " load_logs took " , sw . Elapsed67 p r i n t " s t a r t i n g load_games"8 sw = Stopwatch . StartNew ( )9 games = load_games (game_logs )

10 sw . Stop ( )11 p r i n t " load_games took " , sw . Elapsed1213 p r i n t " s t a r t i n g ca l c_re co rd "14 sw = Stopwatch . StartNew ( )15 games = calc_record (games , "SEA" )16 sw . Stop ( )17 p r i n t " ca l c_re co rd took " , sw . Elapsed

Rasmus Nielsen ([email protected]) Python & IronPython

Page 28: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 2

Listing 7: AOP in Python part 21 de f timed_op (fun , ∗args , ∗∗kwds ) :2 p r i n t " S t a r t i n g " , fun . __name__3 sw = Stopwatch . StartNew ( )4 ret = fun (∗args , ∗∗kwds )5 sw . Stop ( )6 p r i n t fun . __name__ , " took " ,7 sw . Elapsed8 r e t u r n ret

Rasmus Nielsen ([email protected]) Python & IronPython

Page 29: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 3

Listing 8: AOP in Python part 31 game_logs = timed_op (load_logs , file_list )2 games = timed_op (load_games , game_logs )3 record = timed_op (calc_record , games , "SEA" )

Rasmus Nielsen ([email protected]) Python & IronPython

Page 30: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 5

Listing 9: AOP in Python part 51 de f timed_op_decorator (fun ) :2 de f wrapper (∗args , ∗∗kwds ) :3 p r i n t " S t a r t i n g " , fun . __name__4 sw = Stopwatch . StartNew ( )5 ret = fun (∗args , ∗∗kwds )6 sw . Stop ( )7 p r i n t fun . __name__ , " took " , sw . Elapsed8 r e t u r n ret9 r e t u r n wrapper

Rasmus Nielsen ([email protected]) Python & IronPython

Page 31: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 6

Listing 10: AOP in Python part 61 @timed_op_decorator2 de f load_logs (file_names ) :3 # omit ted f o r c l a r i t y45 @timed_op_decorator6 de f load_games (game_logs ) :7 # omit ted f o r c l a r i t y89 @timed_op_decorator

10 de f calc_record (games , team ) :11 # omit ted f o r c l a r i t y

Rasmus Nielsen ([email protected]) Python & IronPython

Page 32: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Aspect-oriented programming in Python part 7

Listing 11: AOP in Python part 71 game_logs = load_logs (file_list )23 games = load_games (game_logs )45 record = calc_record (games , "SEA" )

Rasmus Nielsen ([email protected]) Python & IronPython

Page 33: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Interop with statically typed .NET code (aka C♯ and VB.NET)

Listing 12: ASP.NET Membership in Python1 impor t clr2 clr . AddReference ( "System .Web . S e c u r i t y " )3 from System . Web . Security impor t Membership4 from System . Web . Security impor t MembershipUser56 user = Membership . CreateUser ( "RN" , "12345678" , ←↩

" r n i e @ i t u . dk" )7 user . IsLockedOut = true8 Membership . UpdateUser (user )9

10 users = Membership . GetAllUsers ( )1112 f o r u i n users :13 p r i n t u . UserName

DEMOhttp://www.trypython.org

Rasmus Nielsen ([email protected]) Python & IronPython

Page 34: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Using Python as a scripting language for web applications

DEMOhttp://www.visitmix.com/labs/gestalt/samples/

getting.started/05_final.html

Rasmus Nielsen ([email protected]) Python & IronPython

Page 35: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Numeric performance in CPython, IronPython, and C♯

Replicating “Case study 2: A division-intensive loop” from PeterSestoft: “Numeric performance in C, C♯ and Java”:

The Poor Man’s Logarithm:

11+

12+

13+ . . .+

1n≥ M (1)

Rasmus Nielsen ([email protected]) Python & IronPython

Page 36: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Implementing the Poor Man’s logarithm

Listing 13: PML in C♯1 u s i n g System ;23 c l a s s Program4 {5 s t a t i c vo i d Main ( s t r i n g [ ] args )6 {7 i n t M = 20 ;8 doub l e sum = 0 . 0 ;9 i n t n = 0 ;

1011 wh i l e (sum < M )12 {13 n++;14 sum += 1.0/n ;15 }16 }17 }

Listing 14: PML in Python1 M = 20 ;2 sum = 0.03 n = 0 ;4 wh i l e sum < M :5 n = n + 16 sum += 1.0/n

Rasmus Nielsen ([email protected]) Python & IronPython

Page 37: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

Results

C and C♯ are equivalent cf. Peter Sestoft

IronPython on par with C

CPython twice as slow as C

Rasmus Nielsen ([email protected]) Python & IronPython

Page 38: Rasmus Nielsen (rnie@itu.dk) · The .NET framework library is very big. Particularly the user interface library Windows Forms is very good. IronPython is easy to embed in .NET applications

. . . . . .

References

Books:IronPython in Action by Michael J. Foord and ChristianMuirhead, Manning Publications Co 2009

Web:http://ironpython.net/http://www.trypython.orghttp://www.visitmix.com/labs/gestalt/samples/getting.started/05_final.htmlhttp://www.wingware.com/http://www.icsharpcode.net/OpenSource/SD/

Videos:http://channel9.msdn.com/posts/martinesmann/Pumping-Iron-Dynamic-Languages-on-NET/http://www.youtube.com/watch?v=ujkzfC2lebAhttp://www.youtube.com/watch?v=E_kZDvwofHY

Rasmus Nielsen ([email protected]) Python & IronPython