SlideShare a Scribd company logo
1 of 14
Download to read offline
www.itu.dk 1
High-performance
sheet-defined functions
in spreadsheets
Peter Sestoft
IT University of Copenhagen
SEMS 2014-07-02
With thanks to Thomas S Iversen, Daniel Cortes, Morten Hansen,
Poul Serek, Morten Poulsen, Hui Xu, Mainul Liton, Poul Brønnum,
Tim Garbos, Kasper Videbæk, Jens Hamann, Jonas Druedahl Rask,
Simon Eikeland Timmermann
www.itu.dk 2
The trouble with functions
•  One cannot define functions in a spreadsheet
•  To define new functions, ”experts” use VBA
•  Often very poorly, witness newsgroup
microsoft.public.excel.programming
•  Many (Excel) built-in functions are bad:
–  Week numbers: two kinds, but not ISO standard
•  Possible answers to this mess:
–  ”People should not use spreadsheets”
–  ”Only computer scientists should define functions”
–  ”All necessary functions should be built in”
–  Or: Functions within the spreadsheet metaphor
(Nuñez 2000, Peyton-Jones et al 2003)
www.itu.dk
Problem example: Area of triangles
•  Area of triangle with sides a, b, c is
SQRT(s(s-a)(s-b)(s-c)) where s = (a+b+c)/2
Either (1) compute s in column D:
or (2) try to inline s in the area formula:
Annoying intermediate result
Horrible and error-prone
www.itu.dk
A solution:
Sheet-defined function TRIAREA
Function
sheet
Ordinary
sheet
Input
cells
Output
cell
www.itu.dk 5
How use sheet-defined functions
•  Assumptions
–  End-users understand spreadsheet models
–  End-users do not understand VBA, C#, VB.NET, …
•  Sheet-defined functions in the organization
–  Models are developed in ordinary spreadsheets
–  After a while functions are factored out of models
–  Functions can be further developed interactively
–  An organization can develop and share libraries
–  Without preventing further evolution by users
•  Works only if
–  Sheet-defined functions are fast enough
Dual implementation
•  Ordinary sheets, interpretive evaluation
–  Frequently edited, rarely evaluated (at "recalculation")
•  Function sheets, compiled evaluation
–  Rarely edited, frequently evaluated (at function calls)
–  Run-time code generation permits interactive editing
Runtime code generation
=SQRT(a1*a1+a2*a2)
ldloc 2
ldloc 2
mul
ldloc 3
ldloc 3
mul
add
call Math.Sqrt
fldl 0xfffffff0(%ebp)
fldl 0xfffffff0(%ebp)
fmulp %st,%st(1)
fldl 0xffffffe8(%ebp)
fldl 0xffffffe8(%ebp)
fmulp %st,%st(1)
faddp %st,%st(1)
fsqrt
Spreadsheet
formula
.NET
bytecode
x86 machine
code
My
compiler
JIT
compiler
Result: A very
fast, portable
spreadsheet
implementation
www.itu.dk
New book (next month)
•  Spreadsheet Implementation Technology,
MIT Press, August 2014
8
Peter Sestoft
Spreadsheet
Implementation
Technology
Basics and Extensions
Version 0.99.5 of 2014-05-10
The MIT Press
Cambridge, Massachusetts
London, England
•  A standard spreadsheet
implementation
•  Sheet-defined functions
•  Examples
•  Design choices
•  Scalability and speed
•  Implementation details
•  Funcalc user manual
www.itu.dk 9
Example function: NORMDISTCDF
•  Normal distribution N(0,1) cumulative distribution function
•  As accurate as Excel’s built-in NORMSDIST(z), and faster
Input cell Output cell
NORMDISTCDF generated code
•  Approximately 118 ns/call on 2.66 GHz Intel Core 2
•  VBA: 1760 ns; Excel built-in: 1140 ns; C#: 64 ns; C: 54 ns
0000 ldarg V_0 0068 ldloc.0 0198 div
0004 call ValueToDoubleOrNan 0069 call Double.IsInfinity 0199 add
0009 stloc.s V_6 006e brtrue IL_01a0 019a div
000b ldloc.s V_6 0073 ldloc.0 019b br IL_01a1
000d call Math.Abs 0074 call Double.IsNaN 01a0 ldloc.0
0012 stloc.3 0079 brtrue IL_01a0 01a1 br IL_01a7
0013 ldc.r8 -1 007e ldloc.0 01a6 ldloc.0
001c ldloc.3 007f ldc.r8 7.071 01a7 stloc.s V_5
001d mul 0088 bge IL_0144 01a9 ldloc.s V_6
001e ldloc.3 008d ldloc.s V_4 01ab stloc.0
001f mul 008f ldc.r8 220.206867912376 01ac ldloc.0
0020 ldc.r8 2 0098 ldloc.3 01ad call Double.IsInfinity
0029 div 0099 ldc.r8 221.213596169931 01b2 brtrue IL_01f3
002a call Math.Exp 00a2 ldloc.3 01b7 ldloc.0
002f stloc.s V_4 00a3 ldc.r8 112.07929149787 01b8 call Double.IsNaN
0031 ldloc.3 00ac ldloc.3 01bd brtrue IL_01f3
0032 stloc.0 00ad ldc.r8 33.912866078383 01c2 ldloc.0
0033 ldloc.0 00b6 ldloc.3 01c3 ldc.r8 0
0034 call Double.IsInfinity 00b7 ldc.r8 6.37396220353165 01cc bge IL_01dd
0039 brtrue IL_01a6 00c0 ldloc.3 01d1 ldloc.s V_5
003e ldloc.0 00c1 ldc.r8 0.700383064443688 01d3 call NumberValue.Make
003f call Double.IsNaN 00ca ldloc.3 01d8 br IL_01ee
0044 brtrue IL_01a6 00cb ldc.r8 0.035262496599891 01dd ldc.r8 1
0049 ldloc.0 00d4 mul 01e6 ldloc.s V_5
004a ldc.r8 37 00d5 add 01e8 sub
0053 ble IL_0066 00d6 mul 01e9 call NumberValue.Make
0058 ldc.r8 0 00d7 add 01ee br IL_01f9
0061 br IL_01a1 00d8 mul 01f3 ldloc.0
0066 ldloc.3 00d9 add 01f4 call NumberValue.Make
0067 stloc.0 ... 61 lines left out ... 01f9 ret
Asingle
unwrapping
WrappingWrappingWrapping
Examples: Calendrical functions
•  Excel’s calendar functions are poor
–  Wrong before 1900, no ISO week numbers,
cannot easily find first Monday of month, Easter, …
•  Easy to implement as sheet-defined functions
•  Example: Easter in a given year (1400 ns/call):
By MSc students Xu and
Liton, following
Dershowitz & Reingold
(3rd ed, Cambridge UP)
Input: year
Output:
Easter
fixdate
•  Some other functions:
–  Fixdate to/from day-month-year
–  Fixdate to/from ISO week and ISO year
–  Last/nth Monday (etc) before given date
–  First/nth Monday (etc) after given date
Higher-order functions:
Sheet-defined functions as values
•  New built-ins to manipulate functions
–  CLOSURE(“name”, a1, …) evaluates to a closure:
a partially applied sheet-defined function
–  APPLY(f, b1, …) applies a function value
•  Example function “ndie”, a general n-side die
•  Defining & rolling 6-sided and 20-sided dice
Input cell
Output cell
www.itu.dk
Funsheet: Linking Excel and Funcalc
•  Sheet-defined functions in Excel!
•  Eikeland and Timmermann MSc, June 2014
•  Via Excel DNA, an Excel-.NET bridge
•  Generated code is as fast as Funcalc
•  Call speed Excel -> Funcalc suffers from
general Excel slowness, 11 us/call or so
•  Complete Funcalc functionality: DEFINE,
CLOSURE, APPLY, SPECIALIZE, BENCHMARK
•  Prototype, so still a number of defects
13
www.itu.dk
TO DO: Validation
•  Improve the Excel <-> Funcalc link
•  Demonstrate one application area
•  Fix obvious problems
•  Perform development experiments
•  Perform maintenance experiments
•  ...
•  But experiments is not my area of expertise
14

More Related Content

Similar to High-performance sheet-defined functions in Excel - Peter Sestoft at Sems 2014

Elementary Data Analysis with MS Excel_Day-2
Elementary Data Analysis with MS Excel_Day-2Elementary Data Analysis with MS Excel_Day-2
Elementary Data Analysis with MS Excel_Day-2Redwan Ferdous
 
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362TawnaDelatorrejs
 
Spreadsheets 2003 version
Spreadsheets 2003 versionSpreadsheets 2003 version
Spreadsheets 2003 versionJohan Koren
 
Excel for QA - must know
Excel for QA - must knowExcel for QA - must know
Excel for QA - must knowOlesia Hirnyk
 
Model-based programming and AI-assisted software development
Model-based programming and AI-assisted software developmentModel-based programming and AI-assisted software development
Model-based programming and AI-assisted software developmentEficode
 
PAKDD2016 Tutorial DLIF: Introduction and Basics
PAKDD2016 Tutorial DLIF: Introduction and BasicsPAKDD2016 Tutorial DLIF: Introduction and Basics
PAKDD2016 Tutorial DLIF: Introduction and BasicsAtsunori Kanemura
 
L4 advanced spreadsheet skills
L4 advanced spreadsheet skillsL4 advanced spreadsheet skills
L4 advanced spreadsheet skillsrowenick
 
L4 Advanced Spreadsheet Skills.pptx
L4 Advanced Spreadsheet Skills.pptxL4 Advanced Spreadsheet Skills.pptx
L4 Advanced Spreadsheet Skills.pptxSheenaAFuentes
 
E-Tech L4 Advanced Spreadsheet Skills.pptx
E-Tech L4 Advanced Spreadsheet Skills.pptxE-Tech L4 Advanced Spreadsheet Skills.pptx
E-Tech L4 Advanced Spreadsheet Skills.pptxLeah Condina
 
Technology in maths and maths in technology
Technology  in maths and maths in technologyTechnology  in maths and maths in technology
Technology in maths and maths in technologyshajunisha
 
Feature Engineering
Feature Engineering Feature Engineering
Feature Engineering odsc
 
Introduction to Spreadsheets.ppt
Introduction to Spreadsheets.pptIntroduction to Spreadsheets.ppt
Introduction to Spreadsheets.pptssuser1028f8
 
Performance Tuning Oracle's BI Applications
Performance Tuning Oracle's BI ApplicationsPerformance Tuning Oracle's BI Applications
Performance Tuning Oracle's BI ApplicationsKPI Partners
 
Spreadsheets 2007/2010 version
Spreadsheets 2007/2010 versionSpreadsheets 2007/2010 version
Spreadsheets 2007/2010 versionJohan Koren
 

Similar to High-performance sheet-defined functions in Excel - Peter Sestoft at Sems 2014 (20)

Elementary Data Analysis with MS Excel_Day-2
Elementary Data Analysis with MS Excel_Day-2Elementary Data Analysis with MS Excel_Day-2
Elementary Data Analysis with MS Excel_Day-2
 
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362
BusinessIDNameStreetAddressCityState1001Computer Accessories Etc362
 
Spreadsheets 2003 version
Spreadsheets 2003 versionSpreadsheets 2003 version
Spreadsheets 2003 version
 
Excel for QA - must know
Excel for QA - must knowExcel for QA - must know
Excel for QA - must know
 
MICROSOFT EXCEL.pptx
MICROSOFT EXCEL.pptxMICROSOFT EXCEL.pptx
MICROSOFT EXCEL.pptx
 
Model-based programming and AI-assisted software development
Model-based programming and AI-assisted software developmentModel-based programming and AI-assisted software development
Model-based programming and AI-assisted software development
 
PAKDD2016 Tutorial DLIF: Introduction and Basics
PAKDD2016 Tutorial DLIF: Introduction and BasicsPAKDD2016 Tutorial DLIF: Introduction and Basics
PAKDD2016 Tutorial DLIF: Introduction and Basics
 
cyber_systems.pdf
cyber_systems.pdfcyber_systems.pdf
cyber_systems.pdf
 
Excel.t01
Excel.t01Excel.t01
Excel.t01
 
Excel.t01
Excel.t01Excel.t01
Excel.t01
 
L4 advanced spreadsheet skills
L4 advanced spreadsheet skillsL4 advanced spreadsheet skills
L4 advanced spreadsheet skills
 
L4 Advanced Spreadsheet Skills.pptx
L4 Advanced Spreadsheet Skills.pptxL4 Advanced Spreadsheet Skills.pptx
L4 Advanced Spreadsheet Skills.pptx
 
E-Tech L4 Advanced Spreadsheet Skills.pptx
E-Tech L4 Advanced Spreadsheet Skills.pptxE-Tech L4 Advanced Spreadsheet Skills.pptx
E-Tech L4 Advanced Spreadsheet Skills.pptx
 
Technology in maths and maths in technology
Technology  in maths and maths in technologyTechnology  in maths and maths in technology
Technology in maths and maths in technology
 
Feature Engineering
Feature Engineering Feature Engineering
Feature Engineering
 
Mathcad 15 overview
Mathcad 15 overviewMathcad 15 overview
Mathcad 15 overview
 
Introduction to Spreadsheets.ppt
Introduction to Spreadsheets.pptIntroduction to Spreadsheets.ppt
Introduction to Spreadsheets.ppt
 
MS Excel
MS ExcelMS Excel
MS Excel
 
Performance Tuning Oracle's BI Applications
Performance Tuning Oracle's BI ApplicationsPerformance Tuning Oracle's BI Applications
Performance Tuning Oracle's BI Applications
 
Spreadsheets 2007/2010 version
Spreadsheets 2007/2010 versionSpreadsheets 2007/2010 version
Spreadsheets 2007/2010 version
 

More from semsworkshop

Spreadsheets are models too - Richard Paige at Sems 2014
Spreadsheets are models too - Richard Paige at Sems 2014Spreadsheets are models too - Richard Paige at Sems 2014
Spreadsheets are models too - Richard Paige at Sems 2014semsworkshop
 
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014semsworkshop
 
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014semsworkshop
 
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014semsworkshop
 
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014semsworkshop
 
Tool-Supported 2014 07 sems_limitations_evaluation_practice
Tool-Supported 2014 07 sems_limitations_evaluation_practiceTool-Supported 2014 07 sems_limitations_evaluation_practice
Tool-Supported 2014 07 sems_limitations_evaluation_practicesemsworkshop
 
2014 07 sems_debugging_models
2014 07 sems_debugging_models2014 07 sems_debugging_models
2014 07 sems_debugging_modelssemsworkshop
 

More from semsworkshop (7)

Spreadsheets are models too - Richard Paige at Sems 2014
Spreadsheets are models too - Richard Paige at Sems 2014Spreadsheets are models too - Richard Paige at Sems 2014
Spreadsheets are models too - Richard Paige at Sems 2014
 
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014
MDSheet - Model driven spreadsheets - Jacome Cunha at Sems 2014
 
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014
Dependence tracing techniques for spreadsheets - Sohon Roy at Sems 2014
 
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014
A spreadsheet cell-meaning model for testing - Daniel Kulesz at Sems 2014
 
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014
Using a visual language to create better spreadsheets - Bas Jansen at Sems 2014
 
Tool-Supported 2014 07 sems_limitations_evaluation_practice
Tool-Supported 2014 07 sems_limitations_evaluation_practiceTool-Supported 2014 07 sems_limitations_evaluation_practice
Tool-Supported 2014 07 sems_limitations_evaluation_practice
 
2014 07 sems_debugging_models
2014 07 sems_debugging_models2014 07 sems_debugging_models
2014 07 sems_debugging_models
 

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 

High-performance sheet-defined functions in Excel - Peter Sestoft at Sems 2014

  • 1. www.itu.dk 1 High-performance sheet-defined functions in spreadsheets Peter Sestoft IT University of Copenhagen SEMS 2014-07-02 With thanks to Thomas S Iversen, Daniel Cortes, Morten Hansen, Poul Serek, Morten Poulsen, Hui Xu, Mainul Liton, Poul Brønnum, Tim Garbos, Kasper Videbæk, Jens Hamann, Jonas Druedahl Rask, Simon Eikeland Timmermann
  • 2. www.itu.dk 2 The trouble with functions •  One cannot define functions in a spreadsheet •  To define new functions, ”experts” use VBA •  Often very poorly, witness newsgroup microsoft.public.excel.programming •  Many (Excel) built-in functions are bad: –  Week numbers: two kinds, but not ISO standard •  Possible answers to this mess: –  ”People should not use spreadsheets” –  ”Only computer scientists should define functions” –  ”All necessary functions should be built in” –  Or: Functions within the spreadsheet metaphor (Nuñez 2000, Peyton-Jones et al 2003)
  • 3. www.itu.dk Problem example: Area of triangles •  Area of triangle with sides a, b, c is SQRT(s(s-a)(s-b)(s-c)) where s = (a+b+c)/2 Either (1) compute s in column D: or (2) try to inline s in the area formula: Annoying intermediate result Horrible and error-prone
  • 4. www.itu.dk A solution: Sheet-defined function TRIAREA Function sheet Ordinary sheet Input cells Output cell
  • 5. www.itu.dk 5 How use sheet-defined functions •  Assumptions –  End-users understand spreadsheet models –  End-users do not understand VBA, C#, VB.NET, … •  Sheet-defined functions in the organization –  Models are developed in ordinary spreadsheets –  After a while functions are factored out of models –  Functions can be further developed interactively –  An organization can develop and share libraries –  Without preventing further evolution by users •  Works only if –  Sheet-defined functions are fast enough
  • 6. Dual implementation •  Ordinary sheets, interpretive evaluation –  Frequently edited, rarely evaluated (at "recalculation") •  Function sheets, compiled evaluation –  Rarely edited, frequently evaluated (at function calls) –  Run-time code generation permits interactive editing
  • 7. Runtime code generation =SQRT(a1*a1+a2*a2) ldloc 2 ldloc 2 mul ldloc 3 ldloc 3 mul add call Math.Sqrt fldl 0xfffffff0(%ebp) fldl 0xfffffff0(%ebp) fmulp %st,%st(1) fldl 0xffffffe8(%ebp) fldl 0xffffffe8(%ebp) fmulp %st,%st(1) faddp %st,%st(1) fsqrt Spreadsheet formula .NET bytecode x86 machine code My compiler JIT compiler Result: A very fast, portable spreadsheet implementation
  • 8. www.itu.dk New book (next month) •  Spreadsheet Implementation Technology, MIT Press, August 2014 8 Peter Sestoft Spreadsheet Implementation Technology Basics and Extensions Version 0.99.5 of 2014-05-10 The MIT Press Cambridge, Massachusetts London, England •  A standard spreadsheet implementation •  Sheet-defined functions •  Examples •  Design choices •  Scalability and speed •  Implementation details •  Funcalc user manual
  • 9. www.itu.dk 9 Example function: NORMDISTCDF •  Normal distribution N(0,1) cumulative distribution function •  As accurate as Excel’s built-in NORMSDIST(z), and faster Input cell Output cell
  • 10. NORMDISTCDF generated code •  Approximately 118 ns/call on 2.66 GHz Intel Core 2 •  VBA: 1760 ns; Excel built-in: 1140 ns; C#: 64 ns; C: 54 ns 0000 ldarg V_0 0068 ldloc.0 0198 div 0004 call ValueToDoubleOrNan 0069 call Double.IsInfinity 0199 add 0009 stloc.s V_6 006e brtrue IL_01a0 019a div 000b ldloc.s V_6 0073 ldloc.0 019b br IL_01a1 000d call Math.Abs 0074 call Double.IsNaN 01a0 ldloc.0 0012 stloc.3 0079 brtrue IL_01a0 01a1 br IL_01a7 0013 ldc.r8 -1 007e ldloc.0 01a6 ldloc.0 001c ldloc.3 007f ldc.r8 7.071 01a7 stloc.s V_5 001d mul 0088 bge IL_0144 01a9 ldloc.s V_6 001e ldloc.3 008d ldloc.s V_4 01ab stloc.0 001f mul 008f ldc.r8 220.206867912376 01ac ldloc.0 0020 ldc.r8 2 0098 ldloc.3 01ad call Double.IsInfinity 0029 div 0099 ldc.r8 221.213596169931 01b2 brtrue IL_01f3 002a call Math.Exp 00a2 ldloc.3 01b7 ldloc.0 002f stloc.s V_4 00a3 ldc.r8 112.07929149787 01b8 call Double.IsNaN 0031 ldloc.3 00ac ldloc.3 01bd brtrue IL_01f3 0032 stloc.0 00ad ldc.r8 33.912866078383 01c2 ldloc.0 0033 ldloc.0 00b6 ldloc.3 01c3 ldc.r8 0 0034 call Double.IsInfinity 00b7 ldc.r8 6.37396220353165 01cc bge IL_01dd 0039 brtrue IL_01a6 00c0 ldloc.3 01d1 ldloc.s V_5 003e ldloc.0 00c1 ldc.r8 0.700383064443688 01d3 call NumberValue.Make 003f call Double.IsNaN 00ca ldloc.3 01d8 br IL_01ee 0044 brtrue IL_01a6 00cb ldc.r8 0.035262496599891 01dd ldc.r8 1 0049 ldloc.0 00d4 mul 01e6 ldloc.s V_5 004a ldc.r8 37 00d5 add 01e8 sub 0053 ble IL_0066 00d6 mul 01e9 call NumberValue.Make 0058 ldc.r8 0 00d7 add 01ee br IL_01f9 0061 br IL_01a1 00d8 mul 01f3 ldloc.0 0066 ldloc.3 00d9 add 01f4 call NumberValue.Make 0067 stloc.0 ... 61 lines left out ... 01f9 ret Asingle unwrapping WrappingWrappingWrapping
  • 11. Examples: Calendrical functions •  Excel’s calendar functions are poor –  Wrong before 1900, no ISO week numbers, cannot easily find first Monday of month, Easter, … •  Easy to implement as sheet-defined functions •  Example: Easter in a given year (1400 ns/call): By MSc students Xu and Liton, following Dershowitz & Reingold (3rd ed, Cambridge UP) Input: year Output: Easter fixdate •  Some other functions: –  Fixdate to/from day-month-year –  Fixdate to/from ISO week and ISO year –  Last/nth Monday (etc) before given date –  First/nth Monday (etc) after given date
  • 12. Higher-order functions: Sheet-defined functions as values •  New built-ins to manipulate functions –  CLOSURE(“name”, a1, …) evaluates to a closure: a partially applied sheet-defined function –  APPLY(f, b1, …) applies a function value •  Example function “ndie”, a general n-side die •  Defining & rolling 6-sided and 20-sided dice Input cell Output cell
  • 13. www.itu.dk Funsheet: Linking Excel and Funcalc •  Sheet-defined functions in Excel! •  Eikeland and Timmermann MSc, June 2014 •  Via Excel DNA, an Excel-.NET bridge •  Generated code is as fast as Funcalc •  Call speed Excel -> Funcalc suffers from general Excel slowness, 11 us/call or so •  Complete Funcalc functionality: DEFINE, CLOSURE, APPLY, SPECIALIZE, BENCHMARK •  Prototype, so still a number of defects 13
  • 14. www.itu.dk TO DO: Validation •  Improve the Excel <-> Funcalc link •  Demonstrate one application area •  Fix obvious problems •  Perform development experiments •  Perform maintenance experiments •  ... •  But experiments is not my area of expertise 14