为什么特定算法通过ctypes需要花费更长的时间?

我有一个C程序,用于测量某些算法使用Clock()将元素插入数据结构所需的时间

我所有的各种函数..关闭哈希表,打开哈希表,二进制搜索树,红黑树..当独立执行时,所有这些函数似乎都需要与通过ctypes调用时一样长的时间

除了一个。我的AVL树。 C语言中的200000个元素大约需要100毫秒,而ctypes中的10000个元素大约需要3600毫秒。罪魁祸首是什么?为什么通常会发生这种情况?我认为通过ctypes调用func与运行已编译的可执行文件相同。

我的AVL树:

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
#include "funcs.h"

struct node_m{
    unsigned long int val;
    struct node_m * left;
    struct node_m * right;
    int height;
};

struct node_m * root;
unsigned char flag_g;

int max(struct node_m * x,struct node_m * y){
    int a;
    int b;
    if(x == NULL){
        a = 0;
    } else
        a = x->height;
    if(y == NULL){
        b = 0;
    } else
        b = y->height;
    return (a > b) ? a : b;
}

struct node_m * r_rot(struct node_m * temp){
    struct node_m * temp_d = temp->left;
    struct node_m * temp_dd = temp_d->right;

    temp_d->right = temp;
    temp->left = temp_dd;
    temp->height = 1 + max(temp->left,temp->right);
    temp_d->height = 1 + max(temp_d->left,temp_d->right);

    return temp_d;
}

struct node_m * l_rot(struct node_m * temp){
    struct node_m * temp_d = temp->right;
    struct node_m * temp_dd = temp_d->left;

    temp_d->left = temp;
    temp->right = temp_dd;
    temp->height = 1 + max(temp->left,temp_d->right);

    return temp_d;
}

struct node_m * insert_m(struct node_m * node,unsigned long int val){
    if(node == NULL){
        struct node_m * leaf = malloc(sizeof(struct node_m));
        leaf->height = 1;
        leaf->val = val;
        leaf->left = NULL;
        leaf->right = NULL;
        return leaf;
    }else if(node->val > val){
        node->left = insert_m(node->left,val);
    } else if(node->val < val){
        node->right = insert_m(node->right,val);
    } else {
        return node;
    }

    if(!flag_g) {
        node->height = 1 + max(node->left,node->right);
        int l,r;

        if (node->left == NULL) {
            l = 0;
        } else
            l = node->left->height;

        if (node->right == NULL) {
            r = 0;
        } else
            r = node->right->height;

        int balance = l - r;

        if (balance < -1 && val > node->right->val) {
            node = l_rot(node);
            return node;
        }

        if (balance < -1 && val < node->right->val) {
            node->right = r_rot(node->right);
            node = l_rot(node);
            return node;
        }

        if (balance > 1 && val < node->left->val) {
            node = r_rot(node);
            return node;
        }

        if (balance > 1 && val > node->left->val) {
            node->left = l_rot(node->left);
            node = r_rot(node);
            return node;
        }
    }

    return node;
}

float driver_mytree(int x,unsigned char flag) {
    unsigned long int * randlist = malloc(x * sizeof(int));
    floyd_rand(&randlist,x);
    flag_g = 0;
    root = malloc(sizeof(struct node_m));
    root->height = 1;
    root->left = NULL;
    root->right = NULL;
    root->val = randlist[0];
    clock_t start = clock();
    for(int i = 1; i < x; i++){
        root = insert_m(root,randlist[i]);
    }
    if(flag){
        flag_g = flag;
        start = clock();
        for(int i = x - 1; i > 0; i--){
            root = insert_m(root,randlist[i]);
        }
    }
    clock_t end = clock();

    return (float) (end - start) / CLOCKS_PER_SEC * 1000;
}

我的python测试文件

import ctypes

testlib = ctypes.CDLL("./lib.so")

testlib.driver_mytree.argtypes = [ctypes.c_uint64,ctypes.c_int]
testlib.driver_mytree.restype = ctypes.c_float
my_tree = testlib.driver_mytree

x = my_tree(10000,0)
print(x)

这是我编译单个文件的方式:

gcc -c -std=c11 -fPIC bvs.c    -o mybuild/bvs.o
gcc -c -std=c11 -fPIC my_hash.c    -o mybuild/my_hash.o
gcc -c -std=c11 -fPIC my_tree.c    -o mybuild/my_tree.o
gcc -c -std=c11 -fPIC rand.c    -o mybuild/rand.o
gcc -c -std=c11 -fPIC pcg_basic.c -o mybuild/pcg_basic.o
gcc -c -std=c11 -fPIC their_hash.c    -o mybuild/their_hash.o
gcc -c -std=c11 -fPIC their_tree.c    -o mybuild/their_tree.o


gcc -shared mybuild/pcg_basic.o mybuild/rand.o mybuild/bvs.o mybuild/my_hash.o mybuild/my_tree.o mybuild/their_tree.o mybuild/their_hash.o -o lib.so

pause

注意:我知道我正在将int传递给unsigned char标志,但它可以与其他参数一起使用

mfkkyv123 回答:为什么特定算法通过ctypes需要花费更长的时间?

发现了问题。

它驻留在称为PCG的随机生成函数如何工作的地方。 由于它会产生随机数,并且有一个“内存”用于均匀分布, 因为我将其与我的knuth算法结合使用,以制作唯一随机数组 数字,将陷入一个漫长的循环。

本文链接:https://www.f2er.com/3141504.html

大家都在问