# Time inference difference between C++ and Python

**URL:** <https://forum.opencv.org/t/time-inference-difference-between-c-and-python/14460>\
**Category:** C++\
**Tags:** dnn\
**Created:** [September 6, 2023, 1:21pm UTC](https://forum.opencv.org/t/time-inference-difference-between-c-and-python/14460 "2023-09-06T13:21:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mentejoao](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mentejoao](https://forum.opencv.org/u/mentejoao)\
**Post date:** [September 6, 2023, 1:21pm UTC](https://forum.opencv.org/t/time-inference-difference-between-c-and-python/14460/1 "2023-09-06T13:21:18Z")

</div>

Using the same model, isn’t supposed to C++ inference using OpenCV DNN be faster than using OpenCV DNN in Python?  
I did some tests with BlobFromImage and BlobFromImages and the best results I got it’s about equal results or a little bit faster in Python. I know this could be an abstract discussion about the implementation in both languages, but somehow am I testing in a wrong way? Or am I expecting a result non equivalent to the reality of both languages?  
Code examples:

```auto
blobzin = cv2.dnn.blobFromImages(images, scalefactor=1.0/255, size=(104,104),mean=0, swapRB=False)

modelONNXPeB.setInput(blobzin)

start = time.perf_counter()
output = modelONNXPeB.forward()
end = time.perf_counter()

```

Result Means ~ 1.4 ms  
C++

```auto
cv::Mat blobzin = cv::dnn::blobFromImages(img2Vec, 1.0 / 255, cv::Size2i::Size_(104, 104), 0.0, false, false, CV_32F);

pModelONNXPeB.setInput(blobzin);

auto start_time = std::chrono::high_resolution_clock::now();
auto outputPeB = pModelONNXPeB.forward();
auto end_time = std::chrono::high_resolution_clock::now();

```

Result Means ~ 1.6s  
OpenCV version: 4.8-dev  
Note: I can upload the model, if it’s necessary.

---

<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:** [September 6, 2023, 3:10pm UTC](https://forum.opencv.org/t/time-inference-difference-between-c-and-python/14460/2 "2023-09-06T15:10:25Z")

</div>

> [@mentejoao](#):
>
> isn’t supposed to C++ inference using OpenCV DNN be faster than using OpenCV DNN in Python?

no, is not.

the language _you_ use doesn’t matter.

you just call OpenCV functions. how you call them is irrelevant. how they’re implemented, that matters.

opencv’s DNN module uses one of various backends. they’re all optimized for their respective device types (CPU, GPU).
