A#1R Vers Perf A#1R Introduction III Radu Nicolescu Department of Computer Science University of Auckland 19 July 2018 1 / 25
A#1R Vers Perf A#1R 1 A#1R Short FAQ 2 FP LINQ versatility 3 Performance issues 4 Assignment #1R F# 2 / 25
A#1R Vers Perf A#1R Outline 1 A#1R Short FAQ 2 FP LINQ versatility 3 Performance issues 4 Assignment #1R F# 3 / 25
A#1R Vers Perf A#1R A#1R Short FAQ • Read regex format? Please see the revised/extended handout #2: @” \ s+” or ” \\ s+” • Output file? Write to stdout, i.e. using C#: Console.WriteLine() , F#: printfn , node.js: console.log . The markers will redirect such outputs to actual files: 1 jbon007 . exe b . t x t 7 > jbon007 . log 2 > jbon007 . e r r 2 3 jbon007 . exe b . t x t 7 1 > jbon007 . log 2 > jbon007 . e r r 4 5 jbon007 . exe b . t x t 7 1 > jbon007 . log 2 > &1 PS. In Unix parlance, everything is a file, including stdout... 4 / 25
A#1R Vers Perf A#1R A#1R Short FAQ • Select first? • Pipeline of fluent method chaining: LINQ-like high-order methods can be arranged in any order, as long as the output on one is still compatible with the input of the next. • Alternate query syntax: The order is relevant, but we haven’t discussed this yet . 5 / 25
A#1R Vers Perf A#1R Outline 1 A#1R Short FAQ 2 FP LINQ versatility 3 Performance issues 4 Assignment #1R F# 6 / 25
A#1R Vers Perf A#1R FP LINQ versatility Essentially the high-level same code can be used for: • Processing in memory sequences, e.g. arrays, lists, ... • In LINQ, all sequences are covered by the super-interface IEnumerable • Processing data base tables, views, ... • SQL Server, Azure SQL, MySQL, NoSQL, MongoDB, CosmosDB, ... • Processing web services • SOAP based (XML) • REST based (XML/ATOM or JSON) 7 / 25
A#1R Vers Perf A#1R FP LINQ versatility Essentially the high-level same code can be used for: • Processing in memory sequences, e.g. arrays, lists, ... • In LINQ, all sequences are covered by the super-interface IEnumerable • Processing data base tables, views, ... • SQL Server, Azure SQL, MySQL, NoSQL, MongoDB, CosmosDB, ... • Processing web services • SOAP based (XML) • REST based (XML/ATOM or JSON) 7 / 25
A#1R Vers Perf A#1R FP LINQ versatility Essentially the high-level same code can be used for: • Processing in memory sequences, e.g. arrays, lists, ... • In LINQ, all sequences are covered by the super-interface IEnumerable • Processing data base tables, views, ... • SQL Server, Azure SQL, MySQL, NoSQL, MongoDB, CosmosDB, ... • Processing web services • SOAP based (XML) • REST based (XML/ATOM or JSON) 7 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R Web Services REST • REST = Representational State Transfer... What? • REST = software architectural style that allows you to build highly scalable distributed systems • REST API is simply an Application Programming Interface that adheres to the above principles • How many APIs for Web programming? Almost 20,000 registered! • Sorry! We are only able to look at a few, such as: Web API, ODATA, ... • OData is just one of the many ways to build a REST API – but quite high-level, close to SQL • We use several languages and platforms, so hopefully concepts will surface 8 / 25
A#1R Vers Perf A#1R FP LINQ versatility – Demo code Look, ma, no SQL, no HTTP, no URL, ... 1 var r e s = Orders 2 . OrderBy ( o = > o . OrderID ) 3 . Take (5) 4 . S e l e c t ( o = > new { 5 o . OrderID , 6 o . Customer . CompanyName , 7 EmployeeName = 8 o . Employee . FirstName + ” ” + o . Employee . LastName 9 } ) ; 10 11 r e s .Dump( ” r e s ” ) ; 9 / 25
A#1R Vers Perf A#1R FP LINQ versatility – Demo code Same result from both a local SQl database and from a remote ODATA service: 10 / 25
A#1R Vers Perf A#1R FP LINQ versatility – URL Magic: FP, LINQ, Lambdas ⇒ Auto-generated URL – GET request 1 ” http :// s e r v i c e s . odata . org / Northwind / Northwind . svc /” + 2 ” Orders () ?” + 3 ”$orderby=OrderID &” + 4 ”$top=5 &” + 5 ”$expand=Customer , Employee &” + 6 ”$ s e l e c t=OrderID , Customer/CompanyName , Employee/FirstName 11 / 25
A#1R Vers Perf A#1R FP LINQ versatility – SQL code Magic: FP, LINQ, Lambdas ⇒ Auto-generated SQL code 1 −− Region Parameters 2 DECLARE @p0 NVarChar (1000) = ’ ’ 3 −− EndRegion 4 SELECT [ t1 ] . [ OrderID ] , [ t2 ] . [ CompanyName ] , ( [ t3 ] . [ FirstNa 5 FROM ( 6 SELECT TOP (5) [ t0 ] . [ OrderID ] , [ t0 ] . [ CustomerID ] , [ t0 7 FROM [ Orders ] AS [ t0 ] 8 ORDER BY [ t0 ] . [ OrderID ] 9 ) AS [ t1 ] 10 LEFT OUTER JOIN [ Customers ] AS [ t2 ] ON [ t2 ] . [ CustomerID ] 11 LEFT OUTER JOIN [ Employees ] AS [ t3 ] ON [ t3 ] . [ EmployeeID ] 12 ORDER BY [ t1 ] . [ OrderID ] 12 / 25
A#1R Vers Perf A#1R Outline 1 A#1R Short FAQ 2 FP LINQ versatility 3 Performance issues 4 Assignment #1R F# 13 / 25
A#1R Vers Perf A#1R Data Parallelism – First, sequential versions • Classical for loop: sum the “worked” results over a given array 1 long For ( long [ ] a ) { 2 var s = 0L ; 3 ( var n in a ) { foreach 4 s = s + Work (n ) ; 5 } 6 s ; return 7 } • Equivalent (still) sequential C# LINQ: 1 long Seq ( long [ ] a ) { 2 return 3 a . S e l e c t (n = > Work(n ) ) . Sum ( ) ; 4 } • Which one is faster? 14 / 25
A#1R Vers Perf A#1R Data Parallelism – Look, ma, no threads! • Equivalent parallel C# LINQ: 1 long Par ( long [ ] a ) { 2 return 3 a . A s P a r a l l e l ( ) . S e l e c t (n = > Work(n ) ) . Sum ( ) ; 4 } • Just insert . AsParallel () and the code will automatically scale to use all available cores! • No worries about semaphores, monitors, synchronised regions, locks, deadlocks, ... • All this critical stuff is well encapsulated under the high-order functions... • How fast are all these? 15 / 25
A#1R Vers Perf A#1R Data Parallelism – Look, ma, no threads! • Linqpad results on a lab machine, with 4 cores – results are about twice faster with optimised compiled code 1 . . . For duration : 15905 ms for ( ; ; ) 2 3 . . . Seq duration : 15802 ms LINQ 4 5 . . . Par duration : 4104 ms LINQ parallel • Your conclusions? 16 / 25
Recommend
More recommend