1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Huge difference in time calculation between Python and Julia: how to speed up Python?

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have an issue with this code, written in Python (and used in VSCode, jupiter), used to process some pics of mine:

    import os
    import re
    import numpy as np
    from PIL import Image
    import pandas as pd

    n_abbondanti = [1441440, 1081080, 720720, 665280, 554400, 498960, 332640, 277200,
    221760, 166320, 110880, 83160, 55440, 50400, 45360, 27720, 25200,
    20160, 15120, 10080, 7560, 5040, 2520, 1680, 1260, 840, 720, 360,
    240, 180, 120, 60, 48, 36, 24, 12, 6, 4, 2, 1]

    # Funzione per trovare i divisori
    def f_divisori(t):
    return [i for i in range(1, t+1) if t % i == 0]

    # Carica immagini filtrando per suffisso e ordina per numero
    def load_images_and_numbers2(folder_path, suffix):
    pattern = re.compile(rf"t0_(\d+)_{suffix}\.jpg$")
    files = [f for f in os.listdir(folder_path) if f.endswith(".jpg")]

    file_number_pairs = []
    for f in files:
    match = pattern.match(f)
    if match:
    num = int(match.group(1))
    file_number_pairs.append((num, f))

    file_number_pairs.sort(key=lambda x: x[0])

    images = [np.array(Image.open(os.path.join(folder_path, f))) for (_, f) in file_number_pairs]
    numbers = [num for (num, _) in file_number_pairs]

    return images, numbers

    # Estrai canale colore (R, G, B)
    def extract_color_channel(img, channel):
    if channel == 'R':
    return img[:, :, 0]
    elif channel == 'G':
    return img[:, :, 1]
    elif channel == 'B':
    return img[:, :, 2]
    else:
    raise ValueError("Canale non valido: usa 'R', 'G' o 'B'.")

    # Calcola std dei valori medi su finestre
    def calculate_std_over_windows_gray(img, window_height, window_width):
    h, w = img.shape
    means = [np.mean(img[i:i+window_height, j:j+window_width])
    for i in range(0, h - window_height + 1, window_height)
    for j in range(0, w - window_width + 1, window_width)]
    return np.std(means)

    # Calcola std delle std su finestre
    def calculate_std_std_over_windows_gray(img, window_height, window_width):
    h, w = img.shape
    stds = [np.std(img[i:i+window_height, j:j+window_width])
    for i in range(0, h - window_height + 1, window_height)
    for j in range(0, w - window_width + 1, window_width)]
    return np.std(stds)

    # Funzioni equivalenti a std_over_wind e std_std_over_wind
    def std_over_wind(img):
    h, w = img.shape
    return [calculate_std_over_windows_gray(img, s, s)
    for s in n_abbondanti if s < min(h, w)]

    def std_std_over_wind(img):
    h, w = img.shape
    return [calculate_std_std_over_windows_gray(img, s, s)
    for s in n_abbondanti if s < min(h, w)]

    # Funzione principale equivalente a std_wind_diverse_compat
    def std_wind_diverse_compat(img):
    h, w = img.shape
    max_abundant_h = max([n for n in n_abbondanti if n <= h])
    max_abundant_w = max([n for n in n_abbondanti if n <= w])
    tip = img[:max_abundant_h, :max_abundant_w]

    h_divs = f_divisori(max_abundant_h)
    w_divs = f_divisori(max_abundant_w)

    matrice_devs = np.zeros((len(h_divs), len(w_divs)))
    for i, hd in enumerate(h_divs):
    for j, wd in enumerate(w_divs):
    matrice_devs[i, j] = calculate_std_over_windows_gray(tip, hd, wd)
    matrice_devs[-1, -1] = 0
    return matrice_devs

    # Creazione nomi colonne come in Julia
    def create_window_column_names():
    h, w = 1260, 1260
    max_abundant_h = max([n for n in n_abbondanti if n <= h])
    max_abundant_w = max([n for n in n_abbondanti if n <= w])
    h_divs = f_divisori(max_abundant_h)
    w_divs = f_divisori(max_abundant_w)

    col_names = ["sample_number"]
    for hd in h_divs:
    for wd in w_divs:
    col_names.append(f"window_{hd}x{wd}")
    return col_names

    def rgb_to_gray(img):
    # img: array HxWx3
    return np.dot(img[...,:3], [0.2989, 0.5870, 0.1140])

    def normalize_image(img):
    min_val = np.min(img)
    max_val = np.max(img)
    return (img - min_val) / (max_val - min_val)

    folder_path = r"C:\Photos"
    images_x1, numbers_x1 = load_images_and_numbers2(folder_path, "60x")
    images_x1_G = [extract_color_channel(img, 'G') for img in images_x1]
    images_x1_gray = [rgb_to_gray(img) for img in images_x1]
    column_names = create_window_column_names()

    images_x1_per_sd_Green = [normalize_image(images_x1_G[200:1460, 300:1560].astype(np.float32))
    for i in range(len(images_x1_G))]

    images_x1_per_sd_Gray = [normalize_image(images_x1_gray[200:1460, 300:1560].astype(np.float32))
    for i in range(len(images_x1_gray))]

    matrici_x1_devs_Green = [std_wind_diverse_compat(img) for img in images_x1_per_sd_Green]
    matrici_x1_devs_Gray = [std_wind_diverse_compat(img) for img in images_x1_per_sd_Gray]


    This code has been literally translated from Julia using an AI. I had the necessity to translate it due to continuous lag and crash of Julia in VSCode. The problem, however, is that Julia took only 10-20 minutes to compute it, while Python is taking more than 5 hours (still calculating). How can I improve the speed and the algorithm efficiency? I only knew that using functions is the way, but it is likely not enough.

    Thank you in advance.

    Continue reading...

Compartilhe esta Página