handle python classes #1119
Replies: 5 comments 12 replies
-
|
It would be good to have a list of the different aspects of classes so it is clear:
|
Beta Was this translation helpful? Give feedback.
-
|
Things that are supported can be found in the tests: There are also some tests for unsupported things: Things to consider :
Things to add first:
Things to add second:
Things to add last:
|
Beta Was this translation helpful? Give feedback.
-
|
I was trying to find the best way to implement the classes in c and I found this method is the more readable and close to the concept of classes. #ifndef TESTMOD_H
#define TESTMOD_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct A A;
struct A
{
void (*Constructor)(A*, double);
void (*Destructor)(A*);
void (*function)(A*, double);
double a;
};
void Class_A(A* self, double a);
void function_A(A* self, double x);
void function2_A(A* self, double x);
void function3_A(A* self, double x);
#endifAnd for the implementation of the function we should declare them by this syntax. void Destructor_A(A* self)
{
free(self);
printf("Destructor Called\n");
}
void Constructor_A(A* self, double a)
{
self->a = a;
printf("Contructor Called\n");
}
void function_A(A* self, double x)
{
self->a += x;
}
void function2_A(A* self, double x)
{
for(long long int i = 0; i < 10000000000; i++)
function_A(self, x);
}
void function3_A(A* self, double x)
{
for(long long int i = 0; i < 10000000000; i++)
self->function(self, x);
}
void Class_A(A* self, double a)
{
self->Constructor = Constructor_A;
self->function = function_A;
self->Destructor = Destructor_A;
self->Constructor(self, a); // Call the constructor
}The declaration of the functions are in a way we will avoid name collision eg: void show_time(char *str, struct timespec *tv1, struct timespec *tv2)
{
printf("%s: %ld seconds\n", str, tv2->tv_sec-tv1->tv_sec);
// printf("%s: %ld nano seconds\n", str, tv2->tv_nsec-tv1->tv_nsec);
}
int main(void)
{
A* p = malloc(sizeof(A));
Class_A(p, 0.0);
{
struct timespec tv1, tv2;
timespec_get(&tv1, TIME_UTC);
for(long long int i = 0; i < 10000000000; i++)
p->function(p, 1);
timespec_get(&tv2, TIME_UTC);
show_time("function pointer call timing", &tv1, &tv2);
}
{
struct timespec tv1, tv2;
timespec_get(&tv1, TIME_UTC);
for(long long int i = 0; i < 10000000000; i++)
function_A(p, 1);
timespec_get(&tv2, TIME_UTC);
show_time("function call timing", &tv1, &tv2);
}
{
struct timespec tv1, tv2;
timespec_get(&tv1, TIME_UTC);
function3_A(p, 1);
timespec_get(&tv2, TIME_UTC);
show_time("class function pointer call timing", &tv1, &tv2);
}
{
struct timespec tv1, tv2;
timespec_get(&tv1, TIME_UTC);
function2_A(p, 1);
timespec_get(&tv2, TIME_UTC);
show_time("class function call timing", &tv1, &tv2);
}
p->Destructur(p);
}And here is his output that shows difference between the |
Beta Was this translation helpful? Give feedback.
-
I used the -O3 flag |
Beta Was this translation helpful? Give feedback.
-
|
The main question remaining to be resolved here is whether we should use functions alongside a structure or function pointers inside the structure. My preference would be for functions alongside a structure. I have listed the advantages of each approach below. Please feel free to modify this post to add additional advantages. functions alongside a structure
function pointers
Vote🎉 - functions alongside a structure |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
an initial conversation about supporting python classes in Pyccel. any ideas to be considered ?
Beta Was this translation helpful? Give feedback.
All reactions