CPU Intensive Calculation Examples?

后端 未结 7 763
轻奢々
轻奢々 2020-12-01 05:12

I need a few easily implementable single cpu and memory intensive calculations that I can write in java for a test thread scheduler.

They should be slightly time con

相关标签:
7条回答
  • 2020-12-01 05:46

    A few easy examples of CPU-intensive tasks:

    • searching for prime numbers (involves lots of BigInteger divisions)
    • calculating large factorials e.g. 2000! ((involves lots of BigInteger multiplications)
    • many Math.tan() calculations (this is interesting because Math.tan is native, so you're using two call stacks: one for Java calls, the other for C calls.)
    0 讨论(0)
  • 2020-12-01 05:46

    I was messing around with Thread priority in Java and used the code below. It seems to keep the CPU busy enough that the thread priority makes a difference.

    @Test
    public void testCreateMultipleThreadsWithDifferentPriorities() throws Exception {
        class MyRunnable implements Runnable {
            @Override
            public void run() {
                for (int i=0; i<1_000_000; i++) {
                    double d = tan(atan(tan(atan(tan(atan(tan(atan(tan(atan(123456789.123456789))))))))));
                    cbrt(d);
                }
                LOGGER.debug("I am {}, and I have finished", Thread.currentThread().getName());
            }
        }
        final int NUMBER_OF_THREADS = 32;
        List<Thread> threadList = new ArrayList<Thread>(NUMBER_OF_THREADS);
        for (int i=1; i<=NUMBER_OF_THREADS; i++) {
            Thread t = new Thread(new MyRunnable());
            if (i == NUMBER_OF_THREADS) {
                // Last thread gets MAX_PRIORITY
                t.setPriority(Thread.MAX_PRIORITY);
                t.setName("T-" + i + "-MAX_PRIORITY");
            } else {
                // All other threads get MIN_PRIORITY
                t.setPriority(Thread.MIN_PRIORITY);
                t.setName("T-" + i);
            }
            threadList.add(t);
        }
    
        threadList.forEach(t->t.start());
        for (Thread t : threadList) {
            t.join();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:00

    Multiply two matrices. The matrices should be huge and stored on the disk.

    String search. Or, index a huge document (detect and count the occurrence of each word or strings of alphabets) For example, you can index all of the identifiers in the source code of a large software project.

    Calculate pi.

    Rotate a 2D matrix, or an image.

    Compress some huge files.

    ...

    0 讨论(0)
  • 2020-12-01 06:04
    • Calculate nth term of the fibonacci series, where n is greater than 70. (time consuming)

    • Calculate factorials of large numbers. (time consuming)

    • Find all possible paths between two nodes, in a graph. (memory consuming)

    0 讨论(0)
  • 2020-12-01 06:05

    Ok this is not Java, but this is based on Dhrystone benchmark algorithm found here. These implementations of the algorithm might give you an idea on how is it done. The link here contains sources to C/C++ and Assembler to obtain the benchmarks.

    0 讨论(0)
  • 2020-12-01 06:06
    1. Official RSA Challenge
    2. Unofficial RSA Challenge - Grab some ciphertext that you want to read in plaintext. Let the computer at it. If u use a randomized algorithm, there is a small but non-zero chance that u will succeed.
    0 讨论(0)
提交回复
热议问题