Saturday, July 4, 2020

#1 2020-05-01 05:49:42 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 279

Challenge...

Hi all.

I find this website that compute fractal mandelbrot in different language incl AppleScript.

I wonder if the speed could be better, anyone like a challenge. smile

CODE:
http://www.timestretch.com/article/mand … _benchmark


The user 'Erik' used 867 mhz Powerbook G4, the difference between Script language that time 2007
was much higher and its today. CGI software that support scripting also support python and before
python some use perl and tcl. SoftImage supported jScript when Microsoft bought the company.
JScript was MicroSoft version of JavaScript. At that time no other high end CGI use JavaScript.

AppleScript maybe have never been a Scripting language to build algorithms to do advanced functions. I do know its possible to build a render for CGI in python so I guess its more close to C
and maybe other reasons.

Would it be wrong to assume Apple could if they want to make AppleScript so much more powerful
in api, speed, function, any comment ??

Or

Doesn't the implementation of Apple Events Scripting not allowed to do so...

Regards.

Last edited by Fredrik71 (2020-05-01 09:39:06 am)


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#2 2020-05-01 06:30:38 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Challenge...

Let' s begin with first statement:(* Applescript Version by Erik Wrenholt
I couldn't figure out how to write to stdout
so it buffers the output until the end. *)

Let's add

Applescript:

display dialog mandelbrotString

at end of the script...


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-05-01 08:07:55 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Challenge...

This script is little faster (16% faster) than the posted by author. But the author posts that the time, consumed by Apple-script is 72 seconds. On my machine, the script from the author runs 7 seconds 353 milliseconds. My script runs 6 seconds 403 milliseconds. Tested with the great "Script Geek" application of Shane Stanley.

I don’t think the author has such a slow computer. This is because mine is also low speed. The author, most likely, gives incorrect data... So, AppleScript version should be faster than Python version (place 7 or 8 of the table in the post)

Here is my version:

Applescript:


property BAILOUT : 16
property MAX_ITERATIONS : 1000

on iterate(x, y)
   set cr to y - 0.5
   set zi to x
   set zr to cr
   set zr2 to 0.0
   set zi2 to 0.0
   set temp to 0.0
   repeat with i from 0 to MAX_ITERATIONS - 1
       if zi2 + zr2 > BAILOUT then return i
       set temp to zr * zi
       set zr2 to zr * zr
       set zi2 to zi * zi
       set zr to zr2 - zi2 + cr
       set zi to temp + temp + x
   end repeat
   return 0
end iterate

set t to (current date)

set mandelbrotString to ""
set nl to linefeed
repeat with y from -39 to 38
   set mandelbrotString to mandelbrotString & nl
   repeat with x from -39 to 38
       if iterate(x / 40.0, y / 40.0) = 0 then
           set mandelbrotString to mandelbrotString & "*"
       else
           set mandelbrotString to mandelbrotString & " "
       end if
   end repeat
end repeat

set elapsed to ((current date) - t) as integer

tell application "TextEdit"
   set newDoc to make new document with properties {name:"mandelbrotString"}
   set text of newDoc to mandelbrotString & nl & "Time Elapsed " & elapsed
end tell

Last edited by KniazidisR (2020-05-01 09:44:58 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#4 2020-05-01 08:52:06 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4532

Re: Challenge...

I would change the way datas are displayed:

Applescript:

mandelbrotString & nl & "Time Elapsed " & elapsed
-- display dialog mandelbrotString -- due to limitations of display dialog it displays only a portion of the datas

set reportName to "Mandelbrot.txt"
set targetFile to ((path to desktop as string) & reportName) as «class furl»
my writeto(targetFile, mandelbrotString & nl & "Time Elapsed " & elapsed, «class utf8», false)

tell application "TextEdit"
   activate
   if exists window reportName then close window reportName
   open targetFile
end tell

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)

on writeto(targetFile, theData, dataType, apendData)
   -- targetFile is the path to the file you want to write
   -- theData is the data you want in the file.
   -- dataType is the data type of theData and it can be text, list, record etc.
   -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
   try
       set targetFile to targetFile as «class furl»
       set openFile to open for access targetFile with write permission
       if not apendData then set eof openFile to 0
       write theData to openFile starting at eof as dataType
       close access openFile
       return true
   on error
       try
           close access file targetFile
       end try
       return false
   end try
end writeto

#=====

And of for sure, I would drop every occurence of the variable nl because ASCII character is deprecated for years and I don't see the need for a custom variable when a built in constant “linefeed” is available for years too.

Applescript:

set nl to (ASCII character 10)
repeat with y from -39 to 38 by 1
   set mandelbrotString to mandelbrotString & nl
   repeat with x from -39 to 38 by 1
       if iterate(x / 40.0, y / 40.0) = 0 then
           set mandelbrotString to mandelbrotString & "*"
       else
           set mandelbrotString to mandelbrotString & " "
       end if
   end repeat
end repeat

set elapsed to unixTime() - t
mandelbrotString & linefeed & "Time Elapsed " & elapsed
-- display dialog mandelbrotString -- due to limitations of display dialog it displays only a portion of the datas

set reportName to "Mandelbrot.txt"
set targetFile to ((path to desktop as string) & reportName) as «class furl»
my writeto(targetFile, mandelbrotString & nl & "Time Elapsed " & elapsed, «class utf8», false)

by

Applescript:

-- set nl to (ASCII character 10)
repeat with y from -39 to 38 by 1
   set mandelbrotString to mandelbrotString & linefeed
   repeat with x from -39 to 38 by 1
       if iterate(x / 40.0, y / 40.0) = 0 then
           set mandelbrotString to mandelbrotString & "*"
       else
           set mandelbrotString to mandelbrotString & " "
       end if
   end repeat
end repeat

set elapsed to unixTime() - t
-- mandelbrotString & linefeed & "Time Elapsed " & elapsed
-- display dialog mandelbrotString -- due to limitations of display dialog it displays only a portion of the datas

set reportName to "Mandelbrot.txt"
set targetFile to ((path to desktop as string) & reportName) as «class furl»
my writeto(targetFile, mandelbrotString & linefeed & "Time Elapsed " & elapsed, «class utf8», false)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 1 mai 2020 16:51:24

Offline

 

#5 2020-05-01 09:23:29 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Challenge...

I fixed ASCII character 10 on linefeed. This does not affect the speed of the script, but I agree that it is better to use this constant of AppleScript.

Also, I agree that it is better to display the result in a TextEdit window.  I made this wish too. Without creating a file, because it is not necessary.

Last edited by KniazidisR (2020-05-01 09:48:38 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#6 2020-05-01 06:37:03 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: Challenge...

KniazidisR wrote:

I don’t think the author has such a slow computer.



He does mention it's a "867 mhz Powerbook G4", which would explain it.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2020-05-02 01:26:00 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5233

Re: Challenge...

On my 3.4 Ghz Intel Core i5 iMac running Mojave, there's no material difference in speed between this and KniazidisR's version: somewhere around 3 or 4 seconds. But it's been fun trying.  smile

Applescript:

on iterate(x, y)
   set BAILOUT to 16.0
   set MAX_ITERATIONS to 1000
   
   set cr to y - 0.5
   set ci to x
   set zi to 0.0
   set zr to 0.0
   
   repeat MAX_ITERATIONS times
       set temp to zr * zi
       set zr2 to zr * zr
       set zi2 to zi * zi
       if (zi2 + zr2 > BAILOUT) then return " "
       set zr to zr2 - zi2 + cr
       set zi to temp + temp + ci
   end repeat
   return "*"
end iterate

on mandelbrot()
   script o
       property collector : {}
   end script
   
   set nl to linefeed
   set z to 39.0 / 40.0
   repeat with y from -39 to 38 by 1
       set end of o's collector to nl & iterate(z, y / 40.0)
       repeat with x from -38 to 38 by 1
           set end of o's collector to iterate(x / 40.0, y / 40.0)
       end repeat
   end repeat
   
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to ""
   set mandelbrotString to o's collector as text
   set AppleScript's text item delimiters to astid
   
   return mandelbrotString
end mandelbrot

set t to (current date)
set mandelbrotString to mandelbrot() & "
Time elapsed: "
& (current date) - t

tell application "BBEdit"
   activate
   make new document with properties {name:"mandelbrotString", text:mandelbrotString}
end tell

On my 2 Ghz PowerPC G5 running Leopard, the best timings are:

Original: 30 seconds
KniazidisR: 27 seconds
NG: 24 seconds

This doesn't necessarily mean mine's the fastest, only that it would have been fifteen years ago.  wink


NG

Offline

 

#8 2020-05-02 07:20:22 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 279

Re: Challenge...

I guess most Scripting language that is high level compile to byte code.

AppleScript use single-pass compiling and in this state its fast in compile time, memory limitation and lack on optimise the code for execution when other Scripting language use 2 pass or multi pass and have better ways to handle this optimise of code for performance in runtime.

There is always a bias compare to any systems, devices, compilers, vendors.

Ps.
I remember when AMD vs Intel, and Intel claimed to have better score on CineBench.
But what people didn't know CineBench used Intel compilers that was optimised and used feature
exclusive on Intel processor. And if the execute of process find a non intel processor it will
use other part of the machine code.

So my thinking is... I'm sure Apple together with Intel have made lot of optimised code. So
to change to AMD would I guess not be so easy.

But I find very interesting to compare different Scripting language. smile


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)