Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Jul 19th, 2010 | Syntax: Java | Size: 1.00 KB | Hits: 149 | Expires: Never
Copy text to clipboard
  1. public class Benchmarks {
  2.  
  3.         public static int product(long[] v1, long[] v2) {
  4.                 int sum=1;
  5.         for(int j = 0; j<v1.length;++j)
  6.           sum += v1[j] * v2[j];
  7.         return sum;
  8.     }
  9.         public static int fewerMultiplications(long[] v1, long[] v2) {
  10.                 int sum=1;
  11.         for(int j = 0; j<v1.length;j+=2)
  12.           sum += (v1[j] + v2[j]) * ( v1[j+1] + v2[j+1] );
  13.         return sum;
  14.     }
  15.         public static void main(String[] args) {
  16.                 int N= 1024*32;
  17.                 long[] v1 = new long[N];
  18.                 long[] v2 = new long[N];
  19.                 long before,after;
  20.                 for(int k = 0;k < 5;++k) { // we repeat to be sure
  21.                 before = System.currentTimeMillis();
  22.                 for(int loop = 1; loop<100000;++loop) product(v1,v2);
  23.                 after = System.currentTimeMillis();
  24.                 System.out.println("product "+(after-before));
  25.                 //
  26.                 before = System.currentTimeMillis();
  27.                 for(int loop = 1; loop<100000;++loop) fewerMultiplications(v1,v2);
  28.                 after = System.currentTimeMillis();
  29.                 System.out.println("fewer mults "+(after-before));
  30.                 System.out.println();
  31.                 }
  32.         }
  33. }