Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation from source code
Components
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
About The Best Formula In The World
Architecture details
Benchmarks
Mandelbrot
N-Body
Primes
Books
By Reference Argument Passing
Compatibility between versions
Creating And Using Libraries
Database Datatype Mapping
Database Request Quoting
Date & time management
Dates and calendars
DBus and Gambas
Differences Between Shell And Exec
Differences From Visual Basic
Distributions & Operating Systems
Drag & Drop
DrawingArea Internal Behaviour
External functions datatype mapping
Frequently Asked Questions
Gambas Farm Server Protocol
Gambas Mailing List Netiquette
Gambas Markdown Syntax
Gambas Naming Conventions
Gambas Object Model
Gambas Scripting
Gambas Server Pages
Gambas Unit Testing
Gambas Wiki Markup Syntax
Getting Started With Gambas
Hall Of Fame
Housekeeping, cleaning up
Image Management In Gambas
Including Help Comments in Source Code
Installation from binary packages
Interpreter limits
Introduction
Just In Time Compiler
Just In Time Compiler (old version)
License
Localisation and Internationalization
Mailing Lists & Forums
Naming Conventions
Network Programming
ODBC Component Documentation
PCRE Pattern Syntax
Porting from Gambas 2 to Gambas 3
Previous News
Project Directory Structure
Release Notes
Reporting a problem, a bug or a crash
Rich Text Syntax
Screenshots
Text highlighting definition file syntax
The Program has stopped unexpectedly by raising signal #11
Variable Naming Convention
WebPage Syntax
Web site home page
What Is Gambas?
Window & Form Management
Window Activation & Deactivation
Window Life Cycle
XML APIs
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

Mandelbrot

This benchmark computes and draws 50 times the same mandelbrot figure on the standard output with 0 and 1 characters.

Results

The execution time is the user time added to the system time, as returned by the bash "time" command.

The execution time is computed by redirecting the standard output to /dev/null.

Python Perl Gambas Gambas + JIT
Execution time (s) 35.3 23.2 13.6 0.56
vs. Python 1 0.66 0.39 0.016
vs. Perl 1.52 1 0.59 0.024
vs. Gambas 2.60 1.71 1 0.041
vs. Gambas + JIT 63.04 41.43 24.29 1

Python source code

#!/usr/bin/python

import sys

MAXITER = 50
LIMIT = 4
out = sys.stdout;

def mandelbrot(w, h) :

  xmin = -1.5
  ymin = -1
  invN = 2.0 / w

  checknext = True

  for y in range(h) :
    Ci = y * invN + ymin

    for x in range(w) :
      Zr = 0.0
      Zi = 0.0
      Tr = 0.0
      Ti = 0.0
      Cr = x * invN + xmin
      if (checknext) :
        for k in range(MAXITER) :
          Zi = 2 * Zr * Zi + Ci
          Zr = Tr - Ti + Cr
          Ti = Zi * Zi
          Tr = Zr * Zr
          if (Tr + Ti) > LIMIT :
            break
        if k == MAXITER :
          out.write("1")
        else :
          out.write("0")
          checknext = False
      else :
        for k in range(MAXITER) :
          Zi = 2 * Zr * Zi + Ci
          Zr = Tr - Ti + Cr
          Ti = Zi * Zi
          Tr = Zr * Zr
        if (Tr + Ti) < LIMIT :
          out.write("1")
        else :
          out.write("0")
          checknext = True

    out.write("\n")

for i in range(50) :
  mandelbrot(200, 200)

Perl source code

#!/usr/bin/perl -w

use strict;

my $MAXITER = 50;
my $LIMIT = 4;

sub mandelbrot($$)
{
  my $w = $_[0];
  my $h = $_[1];
  my $xmin = -1.5;
  my $ymin = -1;
  my $invN = 2/$w;

  my $checknext=1;

  for my $y (0..$h-1) {

    my $Ci = $y * $invN + $ymin;

    X:
    for my $x (0..$w-1) {

      my $Zr = 0;
      my $Zi = 0;
      my $Tr = 0;
      my $Ti = 0;

      my $Cr = $x * $invN + $xmin;

      if ($checknext) {

          for (1..$MAXITER) {
            $Zi = 2 * $Zr * $Zi + $Ci;
            $Zr = $Tr - $Ti + $Cr;
            $Ti = $Zi * $Zi;
            $Tr = $Zr * $Zr;

            if ($Tr + $Ti > $LIMIT) {
              print "0";
              next X;
            }
          }

          print "1";
          $checknext = 0;

      } else {

        for (1..$MAXITER) {
          $Zi = 2 * $Zr * $Zi + $Ci;
          $Zr = $Tr - $Ti + $Cr;
          $Ti = $Zi * $Zi;
          $Tr = $Zr * $Zr;
        }

        if ($Tr+$Ti < $LIMIT) {
          print "1";
        } else { # $Tr+$Ti is a large number or overflow ('nan' or 'inf')
          print "0";
          $checknext = 1;
        }

      }

    }

    print "\n";
  }
}

for(1..50) {
  mandelbrot(200, 200);
}

Gambas source code

#!/usr/bin/env gbs3

Private Const MAXITER As Integer = 50
Private Const LIMIT As Integer = 4

Sub Mandelbrot(W As Integer, H As Integer)

  Dim XMin As Float = -1.5
  Dim YMin As Float = -1
  Dim InvN As Float = 2.0 / W
  Dim CheckNext As Boolean = True
  Dim X, Y, K As Integer
  Dim Cr, Ci, Zr, Zi, {Tr}, Ti, T As Float

  For Y = 0 To H - 1

    Ci = Y * InvN + YMin

    For X = 0 To W - 1

      Zr = 0
      Zi = 0
      {Tr} = 0
      Ti = 0
      Cr = X * InvN + XMin

      If CheckNext Then

        For K = 1 To MAXITER
          Zi = 2 * Zr * Zi + Ci
          Zr = {Tr} - Ti + Cr
          Ti = Zi * Zi
          {Tr} = Zr * Zr
          T = {Tr} + Ti
          If IsNan(T) Or If T > LIMIT Then Break
        Next
        If K > MAXITER Then
          Print "1";
          CheckNext = False
        Else
          Print "0";
        Endif

      Else

        For K = 1 To MAXITER

          Zi = 2 * Zr * Zi + Ci
          Zr = {Tr} - Ti + Cr
          Ti = Zi * Zi
          {Tr} = Zr * Zr

        Next

        T = {Tr} + Ti
        If IsNan(T) Or If T > LIMIT Then
          Print "0";
          CheckNext = True
        Else
          Print "1";
        Endif

      Endif

    Next

    Print

  Next

End

Dim I As Integer

For I = 1 To 50
  Mandelbrot(200, 200)
Next