# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Gaussian blur benchmarking
# This notebook compares different implementations of the Gaussian blur filter.
#
# **Note:** benchmarking results vary heavily depending on image size, kernel size, used operations, parameters and used hardware. Use this notebook to adapt it to your use-case scenario and benchmark on your target hardware. If you have different scenarios or use-cases, you are very welcome to submit your notebook as pull-request!

# +
import pyclesperanto_prototype as cle
from skimage import filters
import cupy
import cupyx.scipy.ndimage as ndi
import time

# to measure kernel execution duration properly, we need to set this flag. It will slow down exection of workflows a bit though
cle.set_wait_for_kernel_finish(True)

# selet a GPU with the following in the name. This will fallback to any other GPU if none with this name is found
cle.select_device('RTX')

# +
# test data
import numpy as np

test_image = np.random.random([100, 512, 512])

sigma = 10
# -

# ## clEsperanto

# +
# convolve with pyclesperanto
result_image = None

cl_test_image = cle.push_zyx(test_image)

for i in range(0, 10):
    start_time = time.time()
    result_image = cle.gaussian_blur(cl_test_image, result_image, sigma_x=sigma, sigma_y=sigma, sigma_z=sigma)
    print("pyclesperanto Gaussian duration: " + str(time.time() - start_time))
# -

# ## cupy

# +
# convolve with cupy
result_image = None
cu_test_image = cupy.asarray(test_image)

for i in range(0, 10):
    start_time = time.time()
    result_image = ndi.gaussian_filter(cu_test_image, output=result_image, sigma=sigma)
    cupy.cuda.stream.get_current_stream().synchronize() # we need to wait here to measure time properly
    print("cupy Gaussian duration: " + str(time.time() - start_time))
# -

# ## scikit-image

# +
# convolve with scikit-image
result_image = None

for i in range(0, 10):
    start_time = time.time()
    result_image = filters.gaussian(test_image, output=result_image, sigma=sigma)
    print("skimage Gaussian duration: " + str(time.time() - start_time))  
# -



StarcoderdataJupyterScriptsDedupFiltered