View Javadoc
1   package org.opentrafficsim.draw.egtf;
2   
3   import org.opentrafficsim.animation.egtf.Convolution;
4   import org.opentrafficsim.base.OtsRuntimeException;
5   
6   /**
7    * Performs a benchmark on Convolution using the classic or FFT method.
8    * <p>
9    * Copyright (c) 2025-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author Wouter Schakel
13   */
14  public final class ConvolutionBenchmark
15  {
16  
17      /**
18       * Constructor.
19       */
20      private ConvolutionBenchmark()
21      {
22          //
23      }
24  
25      /**
26       * Program entry point.
27       * @param args the command line arguments (not used)
28       */
29      public static void main(final String... args)
30      {
31          int[] size = new int[] {10, 12, 15, 18, 20, 25, 30, 35, 50, 100, 200, 500, 1000};
32          for (int i = 0; i < size.length; i++)
33          {
34              for (int j = 0; j < size.length; j++)
35              {
36                  if (size[i] * size[j] <= 100000)
37                  {
38                      double[][] a = new double[size[i]][size[i]];
39                      for (int k = 0; k < size[i]; k++)
40                      {
41                          for (int l = 0; l < size[i]; l++)
42                          {
43                              a[k][l] = Math.random();
44                          }
45                      }
46                      double[][] b = new double[size[j]][size[j]];
47                      for (int k = 0; k < size[j]; k++)
48                      {
49                          for (int l = 0; l < size[j]; l++)
50                          {
51                              b[k][l] = Math.random() * 35.0;
52                          }
53                      }
54                      long t1 = System.currentTimeMillis();
55                      double[][] out1 = conv(a, b);
56                      t1 = System.currentTimeMillis() - t1;
57                      long t2 = System.currentTimeMillis();
58                      double[][] out2 = Convolution.convolution(a, b);
59                      t2 = System.currentTimeMillis() - t2;
60                      for (int k = 0; k < size[j]; k++)
61                      {
62                          for (int l = 0; l < size[j]; l++)
63                          {
64                              if (Math.abs(out1[k][l] - out2[k][l]) > 1e-6)
65                              {
66                                  throw new OtsRuntimeException(
67                                          String.format("output unequal: %.16f vs. %.16f", out1[k][l], out2[k][l]));
68                              }
69                          }
70                      }
71                      System.out.println(String.format("a = %d, b = %d: tConv = %dms, tFft = %dms, gain = %dms", size[i], size[j],
72                              t1, t2, t2 - t1));
73                  }
74              }
75          }
76      }
77  
78      /**
79       * Convolution of two matrices using classical method.
80       * @param a the kernel matrix
81       * @param b the data matrix
82       * @return convolution of a over b, same size as b
83       */
84      private static double[][] conv(final double[][] a, final double[][] b)
85      {
86          double[][] out2 = new double[b.length][b[0].length];
87          int fromRow2 = a.length / 2;
88          int fromCol2 = a[0].length / 2;
89          for (int i = 0; i < b.length; i++)
90          {
91              for (int j = 0; j < b[0].length; j++)
92              {
93                  for (int k = 0; k < a.length; k++)
94                  {
95                      for (int l = 0; l < a[0].length; l++)
96                      {
97                          int m = i - k + fromRow2;
98                          int n = j - l + fromCol2;
99                          if (m >= 0 && n >= 0 && m < b.length && n < b[0].length)
100                         {
101                             out2[i][j] += a[k][l] * b[m][n];
102                         }
103                     }
104                 }
105             }
106         }
107         return out2;
108     }
109 
110 }