Forums

Mandelbrot sample code (if statement issue)

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Mandelbrot sample code (if statement issue)

Postby Csaba » Sun Mar 08, 2009 1:02 pm

Hi,

I have an issue with the line
if .calc(x/40, y/40)  # error: here

in the code below. The error message is:

error: COBRA INTERNAL ERROR / FallThroughException / info=TruthExpr-sh(12369, didBindInh=false, didBindInt=false, didStartBindImp=true, didBindImp=false, token=Token(DOT, '.', '.', ln 18, col 9, Mandelbrot.cobra), Treatment=AsIs, type=nil, 12369)

If I replace the if .calc…. with if .doIt (a dummy Boolean function), I get the same compiler error. So what is the correct Cobra syntax here?

Regards
Csaba

# See <!-- m --><a class="postlink" href="http://www.timestretch.com/FractalBenchmark.html">http://www.timestretch.com/FractalBenchmark.html</a><!-- m -->
# 2009-01-13 Csaba Urbaniczky
# if .calc( x/40 , y/40) does not compile
class Program
const bailOut as int = 16
const maxIterations as int = 1000

def doIt as Boolean is shared
return true

def mandelbrot is shared
try
print 'Rendering'
for y as int in -39 :39 : 1
print
for x as int in -39 :39 : 1
# if .doIt , print '*', stop # give same kind of error
if .calc(x/40, y/40) # error: here
print '*', stop
else
print ' ' , stop
catch ex as Exception
print 'mandelbrot EXCEPTION: [ex]'

def calc(ci as number, y as number) as Boolean is shared
try
cr as decimal = y-0.5
zr as decimal = 0.0
zi as decimal = 0.0
for i as int in 0 : .maxIterations :1
zr2 = zr * zr
zi2 = zi * zi
if zi2 + zr2 > .bailOut, return false
temp = zr * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
return true
catch ex as Exception
print 'calc EXCEPTION: [ex]'
return true

def main is shared
try
print 'Cobra Mandelbrot Start [ DateTime.now]'
startTime= DateTime.now
.mandelbrot
print
print 'Cobra elepsed [DateTime.now.subtract(startTime)] seconds'
catch ex as Exception
print 'main EXCEPTION: [ex]'
Last edited by Csaba on Wed Mar 11, 2009 6:34 am, edited 2 times in total.
Csaba
 
Posts: 42

Re: if statement issue

Postby Charles » Sun Mar 08, 2009 5:27 pm

I don't see anything wrong with your syntax and even if there was, Cobra should always give an error instead of an exception.

I'll take a look.

In the mean time, try changing Boolean to bool and see if that helps.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: if statement issue

Postby Charles » Sun Mar 08, 2009 5:40 pm

Yeah, that's it. Change to "bool" and the problem will go away.

I'll fix up the compiler. Btw, if you're doing timings, a couple considerations:

-- Setting up exception handlers may slow down code. At least when your program stabilizes, you may wish to remove your exception handlers. You can also leave them out anyway as they will print if you experience an uncaught exception at run-time.

-- On .NET, the most accurate way to measure time is using a Stopwatch which has a finer granularity than DateTime. In Cobra that would be:
use System.Diagnostics

...
sw = Stopwatch()
sw.start
...
sw.stop
print sw.elapsedMilliseconds

You can look up the docs on it for more details.

I'll be interested to see your final version if you'd like to post it. Maybe we can include it as a sample. I'm also curious to run it on Mono vs. .NET on the same hardware.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: if statement issue

Postby Charles » Sun Mar 08, 2009 5:43 pm

Oh and your final timing should be done with "cobra -turbo ...". See "cobra -h" for info.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Mandelbrot sample

Postby Csaba » Mon Mar 09, 2009 1:08 pm

Hi,

Here is the working Mandelbrot sample with some of my timing. Feel free to use it as a sample.

I did not care about the -turbo optimization, since the Cobra version with float is surprisingly faster than same example in the VB.net code. The try-catch did not impose any measurable penaltie in this example so I kept it. Note the speed difference between using float and decimal. The Ruby (v1.8.6) version requires 22 s (but I think Ruby is using decimal). I havn't tried Python.

Regards
Csaba

# See <!-- m --><a class="postlink" href="http://www.timestretch.com/FractalBenchmark.html">http://www.timestretch.com/FractalBenchmark.html</a><!-- m -->
# 2009-03-09 Csaba Urbaniczky
# 2009-03-09 Test in SciTE
# With float:: 48-128 ms (in SciTE)
# With float & Try Catch &:48-128 ms = same!
# With decimal & Try Catch 6.05 - 6.06 s = very much slower!
# VB.NET 2005 version on the same computer: 175-180 ms
class Program
const bailOut as int = 16
const maxIterations as int = 1000

def mandelbrot is shared
try
print 'Rendering'
for y as int in -39 :39 : 1
print
for x as int in -39 :39 : 1
if .calc(x/40f, y/40f) # error: here
print '*', stop
else
print ' ' , stop
catch ex as Exception
print 'mandelbrot EXCEPTION: [ex]'

def calc(ci as float, y as float) as bool is shared
try
cr as float = y-0.5f
zr as float = 0.0f
zi as float= 0.0f
for i as int in 0 : .maxIterations :1
zr2 = zr * zr
zi2 = zi * zi
if zi2 + zr2 > .bailOut, return false
temp as float = zr * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
return true
catch ex as Exception
print 'calc EXCEPTION: [ex]'
return true

def main is shared
try
print 'Cobra Mandelbrot Start [DateTime.now]'
sw = System.Diagnostics.Stopwatch()
sw.start
.mandelbrot
sw.stop
print
print 'Cobra elapsed: [sw.elapsedMilliseconds] ms'
catch ex as Exception
print 'main EXCEPTION: [ex]'
Csaba
 
Posts: 42

Re: if statement issue in Mandelbrot sample code

Postby Charles » Mon Mar 09, 2009 1:26 pm

Thanks for posting this. I'll definitely incorporate it.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: if statement issue in Mandelbrot sample code

Postby Charles » Tue Mar 10, 2009 4:44 am

I've added this in Cobra under Samples/. Thanks.

I trimmed out some of the explicit types and use "number" type instead of "float" or "decimal" with the "number" set in a directive above. I feel this represents the canonical Cobra style. The timing wasn't changed.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: if statement issue in Mandelbrot sample code

Postby Charles » Tue Mar 10, 2009 4:44 am

Oh and I added some more warnings and error checking to Cobra to avoid the problems that you originally ran into.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Mandelbrot sample code

Postby Csaba » Tue Mar 10, 2009 1:31 pm

Hi,
EDIT: Nice! I’m looking forward to Cobra 1.0 and when Cobra has support in SharpDevelop.

I have now tested this Mandelbrot sample code also with Ruby 1.9.1 and Pyton 3.0.1. The summary so far on my computer is:

Cobra 0.8 float Time / s: 0.09 Rel time: 1
Cobra 0.8 float and Try Catch Time / s: 0.09 Rel time: 1
VB 2005 Time / s: 0.18 Rel time: 2
Python 3.01 Time / s: 3.3 Rel time: 37
Cobra 0.8 decimal Time / s: 6.1 Rel time: 68
Ruby 1.9.1 Time / s: 11.5 Rel time: 128
Ruby 1.8.6 Time / s: 22.0 Rel time: 244

(Impossible to align – spaces disappears)
For other examples, see http://www.timestretch.com/FractalBenchmark.html

Regards
Csaba
Csaba
 
Posts: 42

Re: Mandelbrot sample code

Postby jonathandavid » Mon Mar 16, 2009 5:05 am

Csaba wrote:Hi,
EDIT: Nice! I’m looking forward to Cobra 1.0 and when Cobra has support in SharpDevelop.

I have now tested this Mandelbrot sample code also with Ruby 1.9.1 and Pyton 3.0.1. The summary so far on my computer is:

Cobra 0.8 float Time / s: 0.09 Rel time: 1
Cobra 0.8 float and Try Catch Time / s: 0.09 Rel time: 1
VB 2005 Time / s: 0.18 Rel time: 2
Python 3.01 Time / s: 3.3 Rel time: 37
Cobra 0.8 decimal Time / s: 6.1 Rel time: 68
Ruby 1.9.1 Time / s: 11.5 Rel time: 128
Ruby 1.8.6 Time / s: 22.0 Rel time: 244

(Impossible to align – spaces disappears)
For other examples, see http://www.timestretch.com/FractalBenchmark.html

Regards
Csaba


That's funny, I wouldn't have expected the decimal version to be so far behind. Chuck, are decimal operations really >50 times slower that their float counterparts?
jonathandavid
 
Posts: 159

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 25 guests