如何利用C ++程序的所有内核 [英] How to utilise all cores for C++ program

查看:120
本文介绍了如何利用C ++程序的所有内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Java程序翻译成C ++,与Java相比,C ++的性能非常慢.在活动监视器上,我看到我第一次运行的Java程序使用了90%以上的CPU,而C ++程序使用了大约30%.

I translated a Java program into C++, and I was getting very slow performance from C++ as compared to Java. Looking at the activity monitor, I saw that the Java program, which I ran first, was using over 90% of the CPU, while the C++ program was using around 30%.

看来,JVM无需用户干预即可利用CPU的全部功能.在C ++情况下,我应该做出哪些更改才能更好地使用CPU?

It appears that the JVM is able to harness the full power of the CPU without user intervention. What changes should I make to use the CPU better for the C++ case?

推荐答案

阅读优质的 C ++编程书,然后参见此C ++参考文献.另请阅读您的C ++编译器的文档(也许 GCC ).

Read a good C++ programming book then see this C++ reference. Read also the documentation of your C++ compiler (perhaps GCC).

还要阅读一些操作系统教科书.

考虑使用诸如 POCO Qt Wt .

对于多核处理器,您可以使用 C ++线程.您需要同步它们,例如使用 mutex

With a multi-core processor, you might use C++ threads. You'll need to synchronize them, e.g. using mutex and condition variables.

在GNU Linux上,您可以阅读 高级linux编程 和使用与过程创建和管理相关的 syscalls(2)(例如 fork(2) execve(2)

On GNU Linux, you could read advanced linux programming and use the syscalls(2) related to process creation and management (e..g. fork(2), execve(2), waitpid(2)...) and profiling (see time(7), profil(3), gprof(1), perf(1)).

在Windows上,您需要使用 WinAPI .

On Windows, you need to use the WinAPI.

我将Java程序翻译成C ++

I translated a Java program into C++

通常,这是一个坏主意.学习您的编程语言和操作系统,并为此设计软件.

In general, that is a bad idea. Learn your programming language and your operating system, and design your software for it.

github RefPerSys Firefox OpenJDK ...)

Take inspiration from existing open source software on github or gitlab (e.g. RefPerSys, Firefox, Tensorflow, OpenJDK, Clang static analyzer, Fish ...)

Java实现(例如 JVM s)具有一些类加载器.

Java implementations (e.g. JVMs) have some garbage collection and some class loader.

C ++实现没有任何垃圾回收.因此,请阅读 GC手​​册.请注意,循环引用在C ++中很难有效处理,这可以解释您的性能问题.

C++ implementations do not have any garbage collection. So read the GC handbook. Notice that circular references are hard to handle efficiently in C++, and this could explain your performance issues.

在Linux/x86-64上,您可以使用以下命令加载插件 dlopen(3) libgccjit )

On Linux/x86-64, you might load plugins with dlopen(3) with dlsym(3) (or generate machine code at runtime using asmjit or libgccjit)

这篇关于如何利用C ++程序的所有内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆