github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/adhoc/AdhocPush.java (about)

     1  import java.util.Random;
     2  
     3  public class AdhocPush {
     4      public static boolean isPrime(long n) {
     5          for (long i = 2; i <= n; i++) {
     6              if (i * i > n) return true;
     7              if (n % i == 0) return false;
     8          }
     9          return false;
    10      }
    11  
    12      public static long slow(long n) {
    13          long sum = 0;
    14          for (long i = 0; i <= n; i++) {
    15              sum += i;
    16          }
    17          return sum;
    18      }
    19  
    20      public static long fast(long n) {
    21          long sum = 0;
    22          long root = (long) Math.sqrt(n);
    23          for (long a = 1; a <= n; a += root) {
    24              long b = Math.min(a + root -1, n);
    25              sum += (b - a + 1) * (a + b) / 2;
    26          }
    27          return sum;
    28      }
    29  
    30      public static void run() {
    31          Random r = new Random();
    32          long base = r.nextInt(1000000) + 1;
    33          for (long i = 0; i < 40000000; i++) {
    34              long n = r.nextInt(10000) + 1;
    35              if (isPrime(base + i))
    36                  slow(n);
    37              else
    38                  fast(n);
    39          }
    40      }
    41  
    42      public static void main(String[] args) {
    43          run();
    44      }
    45  }