Java CPU 密集型计算示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3693197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:32:11  来源:igfitidea点击:

CPU Intensive Calculation Examples?

javamultithreadingcpu-usage

提问by MEURSAULT

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

我需要一些易于实现的单 CPU 和内存密集型计算,我可以用 Java 编写这些计算用于测试线程调度程序。

They should be slightly time consuming, but more importantly resource consuming.

它们应该有点耗时,但更重要的是消耗资源。

Any ideas?

有任何想法吗?

采纳答案by dogbane

A few easy examples of CPU-intensive tasks:

CPU 密集型任务的几个简单示例:

  • 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.)
  • 搜索素数(涉及很多 BigInteger 除法)
  • 计算大阶乘,例如 2000!((涉及大量的 BigInteger 乘法)
  • 许多 Math.tan() 计算(这很有趣,因为 Math.tan 是原生的,所以您使用了两个调用堆栈:一个用于 Java 调用,另一个用于 C 调用。)

回答by rwong

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.

压缩一些大文件。

...

...

回答by t0mm13b

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

好的,这不是 Java,但这是基于此处找到的Dhrystone基准算法。该算法的这些实现可能会让您了解它是如何完成的。此处的链接包含 C/C++ 和汇编程序的源代码,以获得基准测试。

回答by emory

  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.
  1. 官方 RSA 挑战赛
  2. 非官方 RSA 挑战赛 - 获取一些您想以明文形式阅读的密文。让计算机在它。如果您使用随机算法,则您成功的可能性很小但非零。

回答by user207421

The CPU soak test for the PDP-11 was tan(atan(tan(atan(...)))etc. Works the FPU pretty hard and also the stack and registers.

PDP-11 的 CPU 浸泡测试tan(atan(tan(atan(...)))等。FPU 非常努力地工作,堆栈和寄存器也是如此。

回答by Rushil Paul

  • 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)

  • 计算斐波那契数列的第 n 项,其中 n 大于 70。(耗时)

  • 计算大数的阶乘。(耗时)

  • 在图中找出两个节点之间的所有可能路径。(内存消耗)

回答by Jim Tough

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.

我在 Java 中搞乱了线程优先级,并使用了下面的代码。似乎让 CPU 保持足够忙碌,以至于线程优先级会有所不同。

@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();
    }
}