Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/core/dylink/test/python-extension/hellomodule.c
1393 views
1
#include "app.h"
2
#include <stdio.h>
3
#include <math.h>
4
#include <time.h>
5
#include <assert.h>
6
7
extern float mysin(float n);
8
9
static PyObject *hello(PyObject *self, PyObject *args) {
10
float s = 0;
11
printf("start hello...\n");
12
printf(
13
"If this takes more than a second, then the function pointer mechanism "
14
"is broken:\n");
15
time_t start, end;
16
time(&start);
17
// ((int)self) % n is just to keep this from getting compiled out:
18
for (int i = 0; i < 1000000 + ((int)self) % 1000; i++) {
19
s += mysin(i);
20
}
21
time(&end);
22
printf("done hello... s=%f\n", s);
23
printf("time = %ld seconds\n",
24
end - start); // this is in *seconds*, so basicaly going to be 0
25
assert(end - start <= 1);
26
// printf("python-wasm: 'hello there ** FROM HELLO!'\n");
27
return PyNone;
28
}
29
30
static struct PyMethodDef module_methods[] = {{"hello", &hello}, {NULL, NULL}};
31
32
struct PyModuleDef _hellomodule = {
33
.m_name = "hello",
34
.m_methods = module_methods,
35
};
36
37
EXPORTED_SYMBOL int PyInit_hello(void) {
38
printf("PyInit_hello\n");
39
// initialize the module:
40
printf("the hello function is at %p\n", &hello);
41
return PyModuleDef_Init(&_hellomodule);
42
}
43