# Why is SURF slower than SIFT when I use features2d to accomplish this two algorithms?

Hi! i just met a doubting question…my code is this:

``````import numpy as np
import cv2
import matplotlib.pyplot as plt
import time

print("debug")

img1 = cv2.imread('rubberwhale1.png',0)          # queryImage

surf = cv2.xfeatures2d_SURF.create()

tic = time.time()
kp = surf.detect(img1)
toc = time.time()
print(f'The total time of the SURF is：：' + str(float(toc - tic)))

sift = cv2.xfeatures2d_SIFT.create()
tic = time.time()

kp = sift.detect(img1)

toc = time.time()
print(f'The total time of the SIFT is：' + str(float(toc - tic)))

``````

i run this script,and i get result like this(i tested many types imgs):

The total time of the SURF is：：1.225409984588623
The total time of the SIFT is：0.03789663314819336

By the way,my opencv-python == 3.4.2.16 opencv-contrib-python== 3.4.2.16.

as i know ，surf should be much faster than sift, why am I getting this result?

who can help me,i will very appreciate he/she! this question confuses me very much:dizzy_face:

1. initialize both
2. run both once on the data
3. now measure time of multiple runs of each

and you might wanna use `time.perf_counter()` for time differences. that is the highest precision clock available.

1 Like

Thank u very much!!! you are right,I have verified that SURF is faster than SIFT,although i didn’t see SURF is threes times than SIFT,but i think it’s maybe parameters problem.thank you again!

With crackwitz’s help,i successed clear up my doubts,this is my code:

``````import cv2
import time

# 1.init
#surf = cv2.xfeatures2d_SURF.create()
#sift = cv2.xfeatures2d_SIFT.create()
surf = cv2.xfeatures2d.SURF_create(400)
sift = cv2.xfeatures2d.SIFT_create()

total_time = 0
experiment_times = 1
for i in range(experiment_times):
tic = time.perf_counter()
kp = surf.detect(img1)
toc = time.perf_counter()
total_time +=(toc - tic)
print(f'The mean time of the SURF is：：' + str(float(total_time / experiment_times)))

total_time = 0
for i in range(experiment_times):
tic = time.perf_counter()
kp = sift.detect(img1)
toc = time.perf_counter()
total_time +=(toc - tic)

print(f'The mean time of the SIFT is：' + str(float(total_time / experiment_times)))
``````

lena.jpg from opencv/sources/samples/data.
when experiment_times = 1,i got:

``````The mean time of the SURF is：：1.3295887999999998
The mean time of the SIFT is：0.03701199999999982
``````

when experiment_times = 3,i got:

``````The mean time of the SURF is：：0.4178568333333334
The mean time of the SIFT is：0.03863946666666666
``````

when experiment_times = 10,i got:

``````The mean time of the SURF is：：0.14091486000000003
The mean time of the SIFT is：0.037559639999999915
``````

when experiment_times = 100,i got:

``````The mean time of the SURF is：：0.03580691399999997
The mean time of the SIFT is：0.04023345399999998
``````

when experiment_times = 300,i got:

``````The mean time of the SURF is：：0.028634289333333295
The mean time of the SIFT is：0.04184386500000007
``````

when experiment_times = 500,i got:

``````The mean time of the SURF is：：0.026373681000000048
The mean time of the SIFT is：0.04070335280000003
``````

if i dont sent 400 to SURF_create,just use :`surf = cv2.xfeatures2d.SURF_create()`,i got:

``````experiment_times = 1
The mean time of the SURF is：：1.2036111

experiment_times = 3
The mean time of the SURF is：：0.4234273333333333

experiment_times = 10
The mean time of the SURF is：：0.14784130999999998

experiment_times = 100
The mean time of the SURF is：：0.03896763600000006

experiment_times = 300
The mean time of the SURF is：：0.032780147666666676

``````

just writing about the experiment in the hope that it will help others who have doubts