# Self-built paralle code is way slow then the sample code

**URL:** <https://forum.opencv.org/t/self-built-paralle-code-is-way-slow-then-the-sample-code/17553>\
**Category:** C++\
**Created:** [May 7, 2024, 8:45pm UTC](https://forum.opencv.org/t/self-built-paralle-code-is-way-slow-then-the-sample-code/17553 "2024-05-07T20:45:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lin\_Zhao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/lin_zhao/32/1863_2.png) [@Lin\_Zhao](https://forum.opencv.org/u/Lin_Zhao)\
**Post date:** [May 7, 2024, 8:45pm UTC](https://forum.opencv.org/t/self-built-paralle-code-is-way-slow-then-the-sample-code/17553/1 "2024-05-07T20:45:33Z")

</div>

I have opencv 4.2 and built the tutorial code: [OpenCV parallel\_for\_](https://docs.opencv.org/4.2.0/d7/dff/tutorial_how_to_use_OpenCV_parallel_for_.html).

Then I compare the time cost with sample code, the sample code output:

> Parallel Mandelbrot: 1.65217 s  
> Sequential Mandelbrot: 11.2684 s  
> Speed-up: 6.82038 X

However, my output is:

> Parallel Mandelbrot: 32.9983 s  
> Sequential Mandelbrot: 174.418 s  
> Speed-up: 5.28567 X

It just super wired, my **CMakeLists.txt** is here:

```auto
cmake_minimum_required(VERSION 3.16.3)
project(simple_test)

add_compile_options(-std=c++11)

## the output location for bin
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

SET(OpenCV_DIR /home/lin/develop/3rd/opencv/install/opencv-4.2.0-test1/lib/cmake/opencv4/)
find_package(OpenCV 4.2.0 REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")

add_executable(opencv_tutorial_parallel src/tutorial_parallel.cpp)
target_link_libraries(opencv_tutorial_parallel ${OpenCV_LIBS})

```

Any suggestion will be great! Thanks all!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 8, 2024, 7:14am UTC](https://forum.opencv.org/t/self-built-paralle-code-is-way-slow-then-the-sample-code/17553/2 "2024-05-08T07:14:06Z")

</div>

speedup is similar. parallelization seems to have worked.

your only issue is optimizations (not) being applied by the compiler.

look at the cmake thingy of the example and compare to yours.

---

<div class="post-metadata">

**Author:** ![Lin\_Zhao](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/lin_zhao/32/1863_2.png) [@Lin\_Zhao](https://forum.opencv.org/u/Lin_Zhao)\
**Post date:** [May 9, 2024, 4:06pm UTC](https://forum.opencv.org/t/self-built-paralle-code-is-way-slow-then-the-sample-code/17553/3 "2024-05-09T16:06:26Z")

</div>

**set(CMAKE\_CXX\_FLAGS “${CMAKE\_CXX\_FLAGS} -O3 -fsee -fomit-frame-pointer -fno-signed-zeros -fno-math-errno -funroll-loops”)**

add this line solved problem, thanks for suggestions
