Created
October 8, 2011 21:29
-
-
Save nagisa/1272907 to your computer and use it in GitHub Desktop.
Running C_SHIT...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <Python.h> | |
static PyObject * shit(PyObject *self, PyObject *args) | |
{ | |
signed long long int number; | |
signed long long int steps; | |
signed long long int i; | |
signed long long int the_biggest = 0; | |
for(i = 0; i < 1000000;i++) { | |
steps = 0; | |
number = i; | |
while (number > 1) { | |
if ( number % 2 == 0) { | |
number = number / 2; | |
steps = steps + 1; | |
} | |
else { | |
number = 3*number + 1; | |
steps = steps + 1; | |
} | |
if (steps > the_biggest) { | |
the_biggest = steps; | |
} | |
} | |
} | |
return Py_BuildValue("l", the_biggest); | |
} | |
PyMethodDef methods[] = { | |
{"shit", shit, METH_VARARGS, "Does shit"}, | |
{NULL, NULL, 0, NULL} | |
}; | |
PyMODINIT_FUNC | |
initc_shit() | |
{ | |
(void) Py_InitModule("c_shit", methods); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
signed long long int number; | |
signed long long int steps; | |
signed long long int i; | |
signed long long int the_biggest; | |
the_biggest = 0; | |
for(i = 0; i < 1000000;i++) { | |
steps = 0; | |
number = i; | |
while (number > 1) { | |
if ( number % 2 == 0) { | |
number = number / 2; | |
steps = steps + 1; | |
} | |
else | |
{ | |
number = 3*number + 1; | |
steps = steps + 1; | |
} | |
if (steps > the_biggest) { | |
the_biggest = steps; | |
} | |
} | |
} | |
printf("%lld \n", the_biggest); | |
return 0; | |
} | |
#[nagisa@godbox x]$ cc c_shit2.c && time ./a.out | |
#524 | |
#real 0m1.121s | |
#user 0m1.083s | |
#sys 0m0.033s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import c_shit | |
print c_shit.shit(); | |
#[nagisa@godbox x]$ python2 setup.py build | |
#running build | |
#running build_ext | |
#[nagisa@godbox x]$ mv ./build/lib.linux-x86_64-2.7/c_shit.so ./c_shit.so | |
#[nagisa@godbox x]$ time python2 run.py | |
#524 | |
#real 0m0.608s | |
#user 0m0.593s | |
#sys 0m0.010s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup, Extension | |
setup(name = "Shit", | |
version = "1.0", | |
ext_modules = [Extension("c_shit", ["c_shit.c"])]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.876s
user 0m0.590s
sys 0m0.030s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.746s
user 0m0.593s
sys 0m0.020s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.618s
user 0m0.600s
sys 0m0.013s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.616s
user 0m0.597s
sys 0m0.013s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.619s
user 0m0.587s
sys 0m0.030s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.620s
user 0m0.597s
sys 0m0.020s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.131s
user 0m1.090s
sys 0m0.040s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.121s
user 0m1.083s
sys 0m0.033s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.116s
user 0m1.077s
sys 0m0.033s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.126s
user 0m1.087s
sys 0m0.037s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.125s
user 0m1.070s
sys 0m0.047s
[nagisa@godbox x]$ time python2 run.py
524
real 0m0.610s
user 0m0.580s
sys 0m0.027s
[nagisa@godbox x]$ time ./a.out
524
real 0m1.122s
user 0m1.090s
sys 0m0.030s