T 3 Conference 2013 - SLUG Fest – March 9th Adrien Bertrand T 3 Conference 2013 - SLUG Fest – March 9th A DVANCED L UA P ROGRAMMING n spire TI - C ODE.

Slides:



Advertisements
Similar presentations
Pages and boxes Building quick user interfaces. learning objectives o Build a quick UI with pages and boxes o understand how pages and boxes work o click.
Advertisements

T 3 Conference March 8th Adrien Bertrand TI- n spire Lua A DVANCED S CRIPTING TECHNIQUES Adrien BERTRAND (« Adriweb »)
Lecture 4 Basic Scripting. Administrative  Files on the website will be posted in pdf for compatibility  Website is now mirrored at:
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters (primarily) 2. “Lua Reference Manual” (included in Lua installation.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Creating a Program In today’s lesson we will look at: what programming is different types of programs how we create a program installing an IDE to get.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Introduction to a Programming Environment
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Lua Matthew Spain | Kerry Zhao. Agenda Overview of Lua Language Details Application Areas Demo Conclusion.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Python.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Copyright ©: SAMSUNG & Samsung Hope for Youth. All rights reserved Tutorials Software: Building apps Suitable for: Advanced.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
INTERMEDIATE WEB DESIGN INTRODUCTORY CSS, JAVASCRIPT AND PHP.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
1 k Jarek Rossignac,  2008 Processing  Install Processing  Learn how to edit, run, save, export, post programs  Understand.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CSS preprocessors What are CSS preprocessors, why do we use a CSS preprocessors, how to use a CSS preprocessor and which CSS preprocessors are being used.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Ch 1. A Python Q&A Session Spring Why do people use Python? Software quality Developer productivity Program portability Support libraries Component.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Prachi Chitnis.  The CSS feel  SDS – Synoptic Display Studio  ADL Converter  PV table, Probe…
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Definition CSS “Short for Cascading Style Sheets, a new feature being added to HTML that gives both Web site developers and users more control over how.
Real-Time Cyber Physical Systems Application on MobilityFirst Winlab Summer Internship 2015 Karthikeyan Ganesan, Wuyang Zhang, Zihong Zheng.
Ali Alshowaish. What is HTML? HTML stands for Hyper Text Markup Language Specifically created to make World Wide Web pages Web authoring software language.
Games Development 2 Lua Scripting CO3301 Week 6. Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
Web Development 101 Presented by John Valance
INTRODUCTION TO CSS. TOPICS TO BE DISCUSSED……….  Introduction Introduction  Features of CSS Features of CSS  Creating Style Sheet Creating Style Sheet.
I Power Higher Computing Software Development Development Languages and Environments.
Videos, the More Tag, Permalinks, and Shortlinks.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Computer Science I Programming in Java (programming using Processing IN Java, using IntelliJ IDE) Classwork/Homework: copy your Processing projects over.
Introduction to OOP CPS235: Introduction.
Getting started with the turtle Find the latest version of this document at
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Language Find the latest version of this document at
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Today's Ninja Challenge: Write Your First Computer Game!
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
3D modeling Computer programs used for developing a mathematical representation of any three-dimensional surface of objects, also called 3D modeling.
windows
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Visit for more Learning Resources
Learning to Program D is for Digital.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
6.001 SICP Further Variations on a Scheme
Catchup. Work on project.
Selection Statements Chapter 3.
Presentation transcript:

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th A DVANCED L UA P ROGRAMMING n spire TI - C ODE OPTIMIZATION & A LTERNATIVE L UA E DITORS Adrien BERTRAND (« Adriweb »)

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th I.C ODE O PTIMIZATION 1.L UA P ERFORMANCE B ENCHMARKS 2.T IPS AND T RICKS 3.N SPIRE -L UA S PECIFIC T HINGS II.A LTERNATIVE L UA E DITORS 1.S IMPLE C ODE E DITORS 2.A N IDE : I NTELLIJ IDEA 3.W ORKING WITH THE TI-N SPIRE S OFTWARE 2

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Credits : « Caspring » website’s wiki (now closed) Lua.org L UA P ERFORMANCE B ENCHMARKS 3

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Localize your functions 4 for i = 1, do local x = math.sin(i) end Accessing global variables takes more time than accessing local ones. Always localize your functions ! The following code is 30% faster : local sin = math.sin for i = 1, do local x = sin(i) end

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Tables optimization 5 for i = 1, do local a = {} a[1] = 1; a[2] = 2; a[3] = 3 end Help Lua know more about the tables you’re going to use ! The following code is almost 3x faster: for i = 1, do local a = {true, true, true} a[1] = 1; a[2] = 2; a[3] = 3 end

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th More Tables optimization 6 Again, help Lua know more about the tables you’re going to use ! Avoid useless rehashes when possible ! polyline = { { x = 10, y = 20 }, { x = 15, y = 20 }, { x = 30, y = 20 },... } polyline = { { 10, 20 }, { 15, 20 }, { 30, 20 },... } polyline = { x = { 10, 15, 20… }, y = { 20, 20… } } Better :Best :Before :

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Other tricks Don’t use unpack() in time-critical code  Unpack them yourself ;-) (get values one by one) Don’t use math.max / min() on big tables in time-critical code  Prefer looping through the list and using comparisons Don’t use math.fmod() for positive numbers  Use the % operator. (On negative numbers, use math.fmod, though) 7

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Other tricks Think twice before using pairs() or ipairs()  When you know the bounds/keys, prefer a simple for i=1,x loop Avoid table.insert() when inserting at the end  Instead, use something like tbl[#tbl+1] = 42 When possible, use closures  (Powerful concept behind functions [returning] in another function) More info : 8

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th T IPS AND T RICKS 9

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Indentation : a prerequisite 10

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Simplify your code 11

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Metatables A metatable is a table which can change the behavior of the table it's attached to. 12 t = {} -- our normal table mt = {} -- our metatable, which contains nothing right now setmetatable(t, mt) -- sets mt to be t's metatable getmetatable(t) -- this will return mt (same as t = setmetatable({}, {}) ) t = setmetatable({}, { __index = function(t, key) if key == "foo" then return 0 else return t[key] end end })

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Metatable example 13 testmap = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {8,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1}, […] {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,9}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} } setmetatable( testmap, { __index = {1} } ) -- makes it so undefined areas will be walls (1). setmetatable( self.theTypes, { __index = function(tbl, key) return tbl[(key%(#tbl))+1] end } )

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 14 More fun with metatables Operator overloading t = setmetatable({ 1, 2, 3 }, { __mul = function(t, nbr) local res = {} for k, v in pairs(t) do res[k] = t[k] * nbr end return res end }) t = t * 2 A table that supports the multiplication operator (*) : __add : Addition (+) __sub : Subtraction (-) __mul : Multiplication (*) __div : Division (/) __mod : Modulos (%) __unm : Unary - (negation) __concat : Concatenation (..) __eq : Equality (==) __lt : Less than (<) __le : Less than or equal to (<=) -- gives : { 2, 4, 6 }

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Memoization : Storing the result of some computation for a given input so that, when the same input is given again, the script simply reuses that previous result. 15 function memoize(f) local mem = {} -- memoizing table setmetatable(mem, {__mode = "v"}) -- weak table return function (x) -- new memoizing version of ’f’ local r = mem[x] if r == nil then -- any previous result ? r = f(x) -- calls original function mem[x] = r -- store result for reuse end return r end loadstring = memoize(loadstring)

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 16 N SPIRE -L UA S PECIFIC T HINGS

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th ← Way too slow ← Not appropriate ← Useless here Do not do anything else than drawing in on.paint() Particularly, here’s what you should avoid in on.paint() image.new(), image.copy(), image.rotate() Events definition (like on.enterKey(), etc.) platform.window:invalidate() Reminder : except if you’re dealing with animations, try not to refresh the screen a lot, but only when needed, it will save CPU and memory ! 17

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Use Classes No need to state the obvious on the advantages Use a screen manager / GUI Toolkit 18 ETK TiMasterBox WZGUILib

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Avoid images when possible Images 6 KB 1 KB 6 images = 26KB6 polygons = 2KB Polygons 19

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Static Width or Height if ww == 793 then gc:setFont("sansserif","bi",20) else gc:setFont("sansserif","bi",11) end if ww > 320 then gc:setFont("sansserif","bi",20) else gc:setFont("sansserif","bi",11) end Other examples : gc:setFont("sansserif", "bi", math.min(255, math.max(6, ww/25)) Re-use later : f_medium = math.min(255, math.max(6, ww/25)) … gc:setFont("sansserif", "bi", f_medium) 20

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Use on.varChange() instead of recalling variables in on.timer() function on.timer() ch = var.recall("ch") platform.window:invalidate() end timer.start(0.1) function on.construction() local v = {"quadrilateral"} vars = {} for i, k in ipairs(v) do vars[k] = var.recall(k) or 1 var.monitor(k) end function on.varChange(list) for _, k in pairs(list) do if vars[k] then vars[k] = var.recall(k) or 1 end platform.window:invalidate() end 21

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 22 S IMPLE C ODE E DITORS

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 23 Notepad++ Windows only Lots of languages Nice set of features Plugins

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 24 TextWrangler / BBEdit Mac only Lots of languages Nice set of features Plugins (for BBEdit)

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th SublimeText Windows/Mac/Linux Lots of languages Nice set of features Customizable Plugins 25

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 26 A N IDE : I NTELLIJ IDEA

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Integrated Development Environment Windows/Mac/Linux Free ! A “million” of features Many plugins 27

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Setting up the beast 1.Download and install Intellij Select the Free Edition 2.Install its Lua plugin 1.Go to Settings > Install Plugin 2.Add the “Lua” one in Browse 3.Setup the Nspire-Lua addon 1.Download it on TI-Planet 2.Extract it somewhere 3.Set it as the project’s SDK 28 +

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Once all that is done, here’s what you’ll have : Nspire-Lua specific auto-completion Inline syntax help for common methods Dynamic API help frame with info from Inspired-Lua 29

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th 30 W ORKING WITH THE TI-N SPIRE S OFTWARE

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th Using the built-in SDK : no worries. Using an external editor : Copy/Paste Use the « preliminary » official tool Use Luna (better choice) Usage : luna[.exe] input.lua ouput.tns Video tutorial : 31

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th C REDITS 32 Jérémy Anselme (“Levak”) Jim Bauwens Steve Arnold John Powers Inspired-Lua.org TI-Planet.org

T 3 Conference SLUG Fest – March 9th Adrien Bertrand T 3 Conference SLUG Fest – March 9th A NY Q UESTIONS ? 33 (original images by TI)