Code Samples/Comparisons

Mark Wieder mwieder at ahsoftware.net
Fri Dec 4 18:18:30 EST 2009


Kevin-

Friday, December 4, 2009, 8:23:16 AM, you wrote:

>> what about using task/code examples from
>> http://shootout.alioth.debian.org/. Revcoders (us, runrev ltd?..)  will

> I think that's a great idea.

Sorry, Kevin, I think it's a Very Bad Idea. The examples there are
hand-coded in each language to be optimized for speed (that's not the
bad idea part). Having this an open-source project to optimize the
code is a good way to get some reasonable benchmarks out of the way of
any given company's purview. And out of an individual's coding prowess
or lack thereof as well.

However... if we're looking for a way to show the readability of code,
that's a different matter altogether. There's no way runrev is going
to come out smelling like a rose if we try to code the Mandelbrot
example, just to pick one. Sure, they can all be coded in revTalk,
most of the examples use multiple threads for speed, something we can
only dream of in runrev. And the math routines are someting I wouldn't
attempt without resorting to an external library for speed.

That said, here's a quick port of the Mandelbrot example from the
Pascal code. On my machine it takes 24 minutes to run. Anyone want to
take a stab at improving it? This is all in a button script:

function CalculatePoint Cx, Cy
    constant Limit = 4
    local Zr, Zi, Ti, Tr
    local tReturn
    
    put 0 into Zr
    put 0 into Zi
    put 0 into Tr
    put 0 into Ti
    
    put false into tReturn
    repeat 50 times
        put 2*Zr*Zi + Cy into Zi
        put Tr - Ti + Cx into Zr
        put Zi * Zi into Ti
        put Zr * Zr into Tr
        if (Tr + Ti >= limit) then
            put true into tReturn
            exit repeat
        end if
    end repeat
    return tReturn
end CalculatePoint

function run n
    local Cy, tStep
    local x, y
    local bits, bit
    local tOutput
    
    var
    put 2/n into tStep
    repeat with y = 0 to n-1
        put y * tStep - 1 into Cy
        put 255 into bits
        put 128 into bit
        repeat with x = 0 to n-1
            if CalculatePoint(x * tStep  - 1.5, Cy) then
                -- try saying this three times fast
                put bits bitXor bit into bits
            end if
             
            if bit > 1 then
                put bit / 2 into bit
            else
                put numtochar(bits) after tOutput
                put 255 into bits
                put 128 into bits
            end if
        end repeat
        if bit < 128 then
            put numtochar(bits bitXor(((bit + bit) mod 255)-1)) after tOutput
        end if
    end repeat
    
    return tOutput
end run

on main pOutputFile, pNum
    open file pOutputFile for write
    
    write "P4" & cr to file pOutputFile
    write pNum & " " & pNum & cr to file pOutputFile
    write run(pNum) to file pOutputFile
    close file pOutputFile
end main

on mouseUp
    local tTime
    
    ask file "where do you want the output file to go?"
    if it is not empty then
        put the milliseconds into tTime
        main it, 16000
        answer (the milliseconds - tTime) / 1000 && "seconds"
    end if
end mouseUp

-- 
-Mark Wieder
 mwieder at ahsoftware.net




More information about the use-livecode mailing list