Experiments with Scripting Languages

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Experiments with Scripting Languages

Roberto Ierusalimschy
We translated to Lua the scripts from the "Experiments with Scripting and 
User-Interface Languages", by Brian W. Kernighan & Christopher J. Van Wyk 
(see at http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html). Here are
the timings for Lua (new version), Lua 3.2, and Perl (version 5.004_04 
built for i386-linux). (We will soon put the scprits in the site.)


		lua	lua3.2	perl

sumloop 100k	0.07	0.14	0.16
sumloop 1000k	0.54	1.23	1.36

array1 10k	0.09	0.11	0.11   (perl = 0.64 with associative arrays)
array1 100k	0.76	0.95	0.94   (perl = 6.25 with associative arrays)
array1 1000k	6.94	8.58	9.23

string 100k	0.13	0.44	0.10
string 1000k	1.40	4.54	1.21

ack 3 6		0.29	0.38	1.73
ack 3 7		1.18	1.52	7.18
ack 3 8		4.76	(*)	30.7    (* = stack overflow)

assoc 10k	0.40	0.44	0.29
assoc 100k	4.18	4.55	2.82

cat (bib)	0.58	0.84	0.74
cat1 (bib)	0.16	0.25			(read whole file)
cat (M)		2.68	4.56	1.81
cat1 (M)	0.22	0.38			(read whole file)

wc (bib)	2.36	2.64	4.54
wc1 (bib)	3.49	3.66			(read whole file)
wc (M)		5.07	6.15	7.37
wc1 (M)		4.70	4.91			(read whole file)

tail (bib)	0.72	1.06	0.98
tail (M)	3.88	5.63	3.31

sum1 100k	1.39	1.75	2.50
sum1 rn100	1.39	1.77	2.55
sum1 1000k	13.7	17.4	24.8


obs: the tests were made in a Linux 2.0.36, Pentium 333, with 128Mbytes.
obs: time = user+system
obs: k=1000 (and not 1024)
obs: `bib' is the King James Bible, and `M' is a huge program (in Lua):
  31102  851820 4460056 bible
 270877  646309 6719734 M

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Experiments with Scripting Languages

DASWorks
Roberto,

>We translated to Lua the scripts from the "Experiments with Scripting and
>User-Interface Languages"

This sounds like it could be an excellent set of examples for "scripting" 
lua.  Would it be possible to post them?  I'm sure many lua newbies would 
like to learn from them.

david

Reply | Threaded
Open this post in threaded view
|

Re: Experiments with Scripting Languages

Nick Trout-2
Even better, would it be possible to have an example script archive on the web
site.... I find I learn much faster and learn more about the qualities of a
particular language from example code than I do from trawling through the
manual.

Perhaps with sections:
1) beginner
2) advanced
3) misc contributions - eg. Life etc.

----- Original Message -----
From: <[hidden email]>
To: "Multiple recipients of list" <[hidden email]>
Sent: Monday, April 10, 2000 2:47 PM
Subject: Re: Experiments with Scripting Languages


> Roberto,
>
> >We translated to Lua the scripts from the "Experiments with Scripting and
> >User-Interface Languages"
>
> This sounds like it could be an excellent set of examples for "scripting"
> lua.  Would it be possible to post them?  I'm sure many lua newbies would
> like to learn from them.
>
> david
>

Reply | Threaded
Open this post in threaded view
|

Re: Experiments with Scripting Languages

Roberto Ierusalimschy
In reply to this post by DASWorks
> Would it be possible to post them?

Ok. Here they are.

-- Roberto

text/plain	/home/roberto/lua/testes/bwk/lua-all

Reply | Threaded
Open this post in threaded view
|

Re: Experiments with Scripting Languages

Roberto Ierusalimschy
In reply to this post by DASWorks
> text/plain      /home/roberto/lua/testes/bwk/lua-all

Sorry about that. Let me try again...

-- Roberto

=====================================================================
  Suite of Lua scripts for "Experiments with Scripting and User-Interface
  Languages", by Brian W. Kernighan & Christopher J. Van Wyk 
  (http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html).

  To change `lim', call "lua lim=XXXX prog.lua"
  For instance, to compute ack(3,6), call
  > lua lim=6 ack.lua
=====================================================================
-- ack.lua
-- ackerman function

lim = tonumber(lim) or 7

function ack(m,  n)
  if (m == 0) then
    return n+1
  elseif (n == 0) then
    return ack(m-1, 1)
  else
    return ack(m-1, ack(m, n-1))
  end
end 

print(ack(3,lim))


=====================================================================
-- array1.lua
-- test for arrays

local n = tonumber(lim) or 100000

local x, y = {}, {}
local i = 1
while i<=n do
  x[i] = i
  i = i+1
end

local j = n
while j>=1 do
  y[j] = x[j]
  j = j-1
end


=====================================================================
-- assoc.lua
-- test for associative arrays
-- This test is very bad for Lua :-(. It takes much more time handling
-- strings (format and convertion from number to string) than in the tables

local n = tonumber(lim) or 10000

local i=1
local X={}
while i<=n do
  X[format("%x", i)] = i
  i = i+1
end

local c = 0

i = n
while i>0 do
  if X[i..''] then c = c+1 end
  i = i-1
end
print(c)
  

=====================================================================
-- cat.lua
-- cat: copies a file from stdin to stdout

local line = read()
while line do
  write(line, "\n")
  line = read()
end


=====================================================================
-- cat1.lua
-- cat: copies a file from stdin to stdout
-- reads the whole file at once

write(read"*a")


=====================================================================
-- string.lua
-- test for string manipulation

local n = tonumber(lim) or 100000

local s

local j = 1
while j<=10 do
  s = "abcdef"
  while strlen(s) <= n do
    s = "123"..s.."456"..s.."789"
    s = strsub(s, strlen(s)/2) .. strsub(s, 1, strlen(s)/2)
  end
  j = j+1
end

print(strlen(s))


=====================================================================
-- sum1.lua
-- add numbers in a file

local s = 0
local n = read"*n"    -- reads a number
while n do
  s = s+n
  n = read"*n"
end
print(s)


=====================================================================
-- sumloop.lua
-- test for basic loops

local n = tonumber(lim) or 1000000
local s = 0
local i = 0
while i<n do
  s=s+1
  i=i+1
end
print(s)


=====================================================================
-- tail.lua
-- invert the lines of a file

local lines = {}
local nl = 0
local line = read()
while line do
  nl = nl+1
  lines[nl] = line
  line = read()
end

while nl>=1 do
  write(lines[nl], "\n")
  nl=nl-1
end




=====================================================================
-- wc.lua
-- print the number of bytes, words, and lines of a file

local cc,lc,wc = 0,0,0
local line = read()
while line do
  cc = cc+strlen(line)
  lc = lc+1
  local _,t = gsub(line, "%S+", "")   -- count words in the line
  wc = wc+t
  line = read()
end

print(lc,wc,cc+lc)   -- adds `lc' to `cc' to include newlines */
  


=====================================================================
-- wc1.lua
-- print the number of bytes, words, and lines of a file
-- read the whole file at once

local t = read"*a"
local cc = strlen(t)
local _,lc = gsub(t, "\n", "\n") 
local _,wc = gsub(t, "%S+", "")
print(lc,wc,cc)

Reply | Threaded
Open this post in threaded view
|

Re: Experiments with Scripting Languages

Luiz Henrique de Figueiredo
In reply to this post by Roberto Ierusalimschy
Here is a program that extracts the scrips that Roberto posted today.

 T=read"*a".."============="
 gsub(T,"=+\n(-- (%w+%.lua).-\n)=",function (a,f)
  print('x',f)
  assert(writeto(f))
  write(a)
  writeto()
  end)

To run, save this script into a file, say a.lua, and then do
	lua a.lua < suite.lua
where suite.lua is the file Roberto posted.

For those interested in simple Lua scripts, the program above is a typical
Lua script that processes text by reading it all in memory and then do whatever
is needed with gsub. Note how the text of each script and the name of the
script are both found using pattern matching.

Enjoy.
--lhf