Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plain old C, to be called via ctypes #4

Open
jsbueno opened this issue Nov 17, 2017 · 2 comments
Open

plain old C, to be called via ctypes #4

jsbueno opened this issue Nov 17, 2017 · 2 comments

Comments

@jsbueno
Copy link

jsbueno commented Nov 17, 2017

we can format this a sa pr later, but it is this thing:

unsigned long long count_double(char *str) {
    char *i, c1, c2;
    unsigned long long  count = 0;

    c1 = *str;
    c2 = *(str + 1);
    for(i = str+1; c2 = *i; i++) {
        count += (c1 == c2);
        c1 = c2;
    }
    return count;
}

Build with:
gcc -shared -o libblacount.so -fPIC blacount.c

And prepare to use from Python with:

import ctypes
blacount = ctypes.cdll.LoadLibrary("./libblacount.so")
blacount.count_double.argtypes = [ctypes.c_char_p]
blacount.count_double.restype = ctypes.c_uint64

(it counts byte-strings only)

@rochacbruno
Copy link
Owner

@jsbueno I also saw this martinxyz@f8e36ab5f9c

Good comparison but as C is counting bytes we can say Rust is playing good as difference is not so significant

@rochacbruno
Copy link
Owner

I added a version using SWIG if you think using ctypes will perform better please send a PR :)

-------------------------------------------------------------------------------------------------
Name (time in us)                     Min                    Max                   Mean          
-------------------------------------------------------------------------------------------------
test_rust_bytes_once             476.7920 (1.0)         830.5610 (1.0)         486.6116 (1.0)    
test_c_swig_bytes_once           795.3460 (1.67)      1,504.3380 (1.81)        827.3898 (1.70)   
test_rust_once                   985.9520 (2.07)      1,483.8120 (1.79)      1,017.4251 (2.09)   
test_numpy                     1,001.3880 (2.10)      2,461.1200 (2.96)      1,274.8132 (2.62)   
test_rust                      2,555.0810 (5.36)      3,066.0430 (3.69)      2,609.7403 (5.36)   
test_regex                    24,787.0670 (51.99)    26,513.1520 (31.92)    25,333.8143 (52.06)  
test_pure_python_once         36,447.0790 (76.44)    48,596.5340 (58.51)    38,074.5863 (78.24)  
test_python_comprehension     49,166.0560 (103.12)   50,832.1220 (61.20)    49,699.2122 (102.13) 
test_pure_python              49,586.3750 (104.00)   50,697.3780 (61.04)    50,148.6596 (103.06) 
test_itertools                56,762.8920 (119.05)   69,660.0200 (83.87)    58,402.9442 (120.02) 
-------------------------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants