At present, the computer's CPU is generally multi-core, and many programs have not been optimized for multiple cores, so there is no way to make full use of the CPU performance.
For example, if a while (1) loop is written in vs2010, 100% of the CPU runs on the old computer, and 25% of the CPU runs on the new 4-core computer, it seems that it cannot be higher.
In many cases, OpenMP can be used to provide multi-core processing capabilities. However, after I add the OpenMP optimization command to the program, I still cannot run it to 100%. Maybe I am wrong.
Therefore, I am not using OpenMP here, but using multi-threaded methods for computation acceleration.
For example, to process an image of 10000*10000 pixels, you can open four threads, each of which processes 2500*10000 pixels, respectively, so that the speed can be increased by four times.
The experimental results also verify the method mentioned above, and the speed of improvement is almost 4 times.
Of course, if it is 8 or 16 cores and 8 or 16 threads are opened, it can be increased by 8 or 16 times.
Image algorithms are generally easy to write in parallel. I use the freeimage Image Library, and the CPU usage is I5 4590.
Processing 10000*10000 of images takes about ms for a single thread, and about ms for four threads, which is basically 4 times higher.
The program may be able to open more threads, but I haven't tried it yet.
The Code is as follows:
# Include <iostream> # include <windows. h> # include <process. h> # include <time. h> # include "freeimage. H" using namespace STD;
// Set it to a global variable. free_image_format fif; fibitmap * IMG; fibitmap * Re;
Int h; int W; int BPP; void Init () {string name = "img.jpg"; fif = freeimage_getfiletype (name. c_str (); IMG = freeimage_load (FIF, name. c_str (); H = freeimage_getheight (IMG); W = freeimage_getwidth (IMG); bpp = freeimage_getbpp (IMG); Re = freeimage_allocate (W, H, BPP );} void calc (INT ymin, int Ymax) {for (INT y = ymin; y <Ymax; y ++) {for (INT x = 0; x <W; X ++) {rgbquad color; freeimage_getpixelcolor (IMG, X, Y, & color); color. rgbblue-= 255; color. rgbgreen-= 255; color. rgbred-= 255; freeimage_setpixelcolor (Re, X, Y, & color) ;}} void calcall () {calc (0, H );} // each thread processes the image's 1/4 void calc1 (pvoid PARAM) {calc (0, INT (H/4);} void calc2 (pvoid PARAM) {calc (INT (H/4), INT (H/2);} void calc3 (pvoid PARAM) {calc (INT (H/2 ), INT (3 * H/4);} void calc4 (pvoid PARAM) {calc (INT (3 * H/4), H);} void main () {Double Start, end; handle hthread [4]; Init (); Start = clock (); calcall (); End = clock (); cout <End-start <Endl; Start = clock (); hthread [0] = (handle) _ beginthread (calc1, 0, null ); hthread [1] = (handle) _ beginthread (calc2, 0, null); hthread [2] = (handle) _ beginthread (calc3, 0, null ); hthread [3] = (handle) _ beginthread (calc4, 0, null); waitformultipleobjects (4, hthread, true, infinite); End = clock (); cout <End-start <Endl; freeimage_unload (IMG); freeimage_unload (re); System ("pause ");}