Optimising a C Algorithm having ported to Python

Baz

I would like to write a Real-time Audio to Pitch algorithm in Python and it looks like the Yin Algorithm solves this problem. I have found a number of C implementations of Yin, and having tried one of them out on my voice it works as expected and in real time. I have started porting it to Python but I can see that it is around 100 times slower - so no longer real.time. I am using a 2.3 GHz Quad-Core Intel Core i7.

Below is a simulation of the algorithm, in both C and Python, to give an idea of the computation involved. Each simulation is calculating the pitch for 90,000 samples. The C function takes 2 seconds and the Python 200 seconds to complete.

C code

#include <stdio.h>
#include <stdint.h>
#include <time.h>

int main(int argc, char** argv) {
    int buffer_length = 150;
    float sec = 0;
    float* buffer = malloc(sizeof(float)*buffer_length);
    
    clock_t before = clock();

    int j;
    int16_t tau;
    int16_t i;
    float delta;
    
    for(j = 0; j < 90000; j++){
        for(tau = 0 ; tau < 75; tau++){
            for(i = 0; i < 75; i++){
                delta = buffer[i] - buffer[i + tau];
                buffer[tau] += delta * delta;
            }
        }    
    }
    
    clock_t difference = clock() - before;
    sec = difference / (float )CLOCKS_PER_SEC;
    printf("%f\n",sec);
    
    return 0;
}

Python code

import time

buffer = [0.0]*150

ts = time.time()

for j in range(90000):
    for tau in range(75):
        for i in range(75):
            delta = buffer[i] - buffer[i + tau]
            buffer[tau] += delta * delta

print(time.time()-ts)

I was thinking of using a sampling rate of 8KHz but possibly a higher rate if possible. Should it be possible to vastly improve the performance of the Python code and if so how would I go about doing this? If not, then I guess writing a C library with a Python Wrapper is my only option.

vencaslac

This looks like a job for numba. The issue is that python for loops are significantly slower than C loops. Using numba you should be able to speed them up quite dramatically since you've written them yourself and there are no external dependencies (which have been known to mess with numba).

Just use the njit decorator and wrap your nested for loops in a function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Optimising a basic python method

Optimising list creation in Python

Optimising while loops in C?

Algorithm for optimising compound index search in MongoDb

Optimising generic backtracking algorithm for N Queens

Optimising concatenation in Python on Pandas dataframe

Optimising Performance of Codility Flags Python

Optimising Python dictionary access code

Optimising compilers mindset for a Python programmer

Vectorization/optimising for loop with numpy in Python

subtraction of lists in python, optimising speed

Optimising a fibonacci sequence generator python

Having an issue with an insertion sort algorithm python

Having some errors, learning c (Euclidean Algorithm)

Optimising mysql insert query via python

Optimising an iterrows loop with multiple column references in Python

Optimising Python/Numpy code used for Simulation

Optimising C++ code block containing for and if

Optimising the process of a Huge List<T> in C#

malloc of a structure in C ( ported from C++ )

Tic tac toe Minimax Algorithm Having Weird Behavior (C++)

NULL vs nullptr in a ported c header

use "python -c" command having a for loop

Optimising python code by not referring the variable which refers to other variable and so on

Why isn't python optimising the function declaration out of the loop?

Optimising a julia one-liner to make it as fast as python

Optimising Python script for scraping to avoid getting blocked/ draining resources

Optimising EV Charging Station Selection using PuLP in Python

Python Script with timezone fails when back ported to python 2.7

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive