Java mandel code for Java benchmarking

2004-06-21
2004-06-22
  • Daniele Paccaloni

    public class JMandel {
       
        static int[] rowBuffer_ = new int[500];
        static int w_ = 500;
        static int h_ = 500;
        static int maxi_ = 9999;
        static double ax_ = -2.0f;
        static double ay_ = -1.5f;
        static double ex_ =  1.0f;
        static double ey_ =  1.5f;
        static double sx_ = (ex_ - ax_) / ((double) w_);
        static double sy_ = (ey_ - ay_) / ((double) h_);

        static long msStart_;
        static long msEnd_;
        static long itersTotal_ = 0;
     

        public static void main(String[] args) {
          msStart_ = System.currentTimeMillis();
          for (int y=0; y<h_; y++) {
            calcPixelRow(y, maxi_);
          }
          msEnd_ = System.currentTimeMillis();
          long msTotal = msEnd_ - msStart_;
          double its = ((double)itersTotal_) / (double)msTotal;
          System.out.println("Runtime ms=" + msTotal + " " + its/1000.0 + " MegaIters per second");
        }

        public static boolean calcPixelRow(int row, int maxi) {

          // C row calculation routine
          // Calc vars
          double cx = ax_;
          double cy = ay_ + sy_*((double)row);
          double zx, zy;
          double zx2, zy2;

           for (int x=0; x<w_; x++) {
            // Calc Pixel
            zx = cx;
            zy = cy;
            int i;
            for (i=0; i<maxi; i++) {
              zx2 = zx*zx;
              zy2 = zy*zy;
              if ((zx2 + zy2) > 4) break;
              zy = 2*zx*zy;
              zx = zx2 - zy2;
              zx += cx;
              zy += cy;
            }
            cx += sx_;
            itersTotal_ += i;
            rowBuffer_[x] = i;
          }

          return true;
        }
    }

     
    • Daniele Paccaloni

      import java.io.*;

      public class JMandel {
         
          static int[] bmpHeader_ = { 0x42, 0x4d, 0xc8, 0xd4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x04, 0x00, 0x00, 0x28, 0x00,
                                      0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00,
                                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
          static int[][] rowBuffer_ = new int[500][500];
          static int w_ = 500;
          static int h_ = 500;
          static int maxi_ = 9999;
          static double ax_ = -2.0f;
          static double ay_ = -1.5f;
          static double ex_ =  1.0f;
          static double ey_ =  1.5f;
          static double sx_ = (ex_ - ax_) / ((double) w_);
          static double sy_ = (ey_ - ay_) / ((double) h_);
         
          static long msStart_;
          static long msEnd_;
          static long itersTotal_ = 0;
         
          public static void main(String[] args) {
              msStart_ = System.currentTimeMillis();
              for (int y=0; y<h_; y++) {
                  calcPixelRow(y, maxi_);
                  for (int x=0; x<w_; x++) itersTotal_ += rowBuffer_[y][x];
              }
              msEnd_ = System.currentTimeMillis();
              long msTotal = msEnd_ - msStart_;
              double its = ((double)itersTotal_) / (double)msTotal;
              System.out.println("Runtime ms=" + msTotal + " " + its/1000.0 + " MegaIters per second");
              // Save the image as 256 colors BMP.
              try {
                  BufferedWriter w = new BufferedWriter(new FileWriter("img.bmp"));
                  // Write BMP header.
                  for (int i=0; i<bmpHeader_.length; i++) w.write(bmpHeader_[i]);
                  // Write palette.
                  for (int i=0; i<256; i++) { w.write(0); w.write(255-i); w.write(255-i); w.write(0); }
                  w.write(0); w.write(0);
                  // Write image.
                  for (int y=0; y<h_; y++) {
                      for (int x=0; x<w_; x++) {
                          int v = rowBuffer_[y][x];
                          // System.out.println(""+ x + "," + y);
                          if (v > 255) v = 255;
                          w.write(v);
                      }
                  }
                  w.flush();
              }
              catch (Exception e) {
                  System.out.println("ERROR WRITING");
              }
          }
         
          public static boolean calcPixelRow(int row, int maxi) {
              // Java row calculation routine
              double cx = ax_;
              double cy = ay_ + sy_*((double)row);
              double zx, zy;
              double zx2, zy2;
             
              for (int x=0; x<w_; x++) {
                  // Calc Pixel
                  zx = cx;
                  zy = cy;
                  int i;
                  for (i=0; i<maxi; i++) {
                      zx2 = zx*zx;
                      zy2 = zy*zy;
                      if ((zx2 + zy2) > 4) break;
                      zy = 2*zx*zy;
                      zx = zx2 - zy2;
                      zx += cx;
                      zy += cy;
                  }
                  cx += sx_;
                  rowBuffer_[row][x] = i;
              }
             
              return true;
          }
      }

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks