达梦数据库-学习-07-C外部函数创建,及与Plsql自定义函数效率对比
一、测试环境信息名称描述操作系统CentOS Linux release 7.9.2009 (Core)达梦数据版本V8 1-2-38-21.07.09-143359-10018-ENT二、测试步骤(1)修改参数,重启数据库sp_set_para_value(2,‘ENABLE_EXTERNAL_CALL’,1);(2)查看是否修改成功,为1表示成功select PARA_NAME,PARA_VA
·
一、测试环境信息
名称 | 描述 |
---|---|
操作系统 | CentOS Linux release 7.9.2009 (Core) |
达梦数据版本 | V8 1-2-38-21.07.09-143359-10018-ENT |
二、测试步骤
(1)修改参数,重启数据库
sp_set_para_value(2,'ENABLE_EXTERNAL_CALL',1);
(2)查看是否修改成功,为1表示成功
select PARA_NAME,PARA_VALUE from v$dm_ini where PARA_NAME='ENABLE_EXTERNAL_CALL';
(3)C外部函数源码–实现0到某数的累加
vim multithreading_v2.c添加如下内容:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
typedef struct Test_Struct
{
int id;
long num1;
long num2;
long result;
struct Test_Struct *next;
} TS;
void *ComputeSum(void *args);
TS* CreateHeadList();
void InsertListData(TS* head, int p_id, long p_num1, long p_num2, long p_result);
TS* GetListDataP(TS* head, int p_id);
char* PrintfList(TS* head);
char* MULTITHREADING(int pthread_num, long num);
/*
void main()
{
long num = 100000000;
int pthread_num = 3;
MULTITHREADING(pthread_num, num);
}
*/
char* MULTITHREADING(int pthread_num, long num)
{
int i;
long temp = 0;
TS *ts = CreateHeadList();
pthread_t *th_arr = (pthread_t *)malloc(sizeof(pthread_t) * pthread_num);
for(i=0; i<pthread_num; i++)
{
if(i == pthread_num - 1)
{
InsertListData(ts, i, temp, num - ts->num1, 0);
}
else
{
InsertListData(ts, i, temp, temp + (num / pthread_num), 0);
temp = temp + (num / pthread_num) + 1;
}
pthread_create(&(th_arr[i]),NULL,ComputeSum,GetListDataP(ts,i));
}
for(i=0; i<pthread_num; i++)
{
pthread_join(th_arr[i],NULL);
}
return PrintfList(ts);
}
void *ComputeSum(void *args)
{
TS* ts = (TS*)malloc(sizeof(TS));
ts = args;
long i;
for(i = ts->num1; i <= ts->num2; i++)
{
ts->result = ts->result + i;
}
//printf("pthread_id(%d), num1 : %4ld, num2: %4ld, result : %ld\n",ts->id,ts->num1,ts->num2,ts->result);
return NULL;
}
TS* CreateHeadList()
{
TS* head = (TS*)malloc(sizeof(TS));
head->id = -1;
head->num1 = 0;
head->num2 = 0;
head->result = 0;
head->next = NULL;
return head;
};
void InsertListData(TS* head, int p_id, long p_num1, long p_num2, long p_result)
{
TS* p = head;
while (p->next != NULL)
{
p = p->next;
}
TS* q = (TS*)malloc(sizeof(TS));
q->id = p_id;
q->num1 = p_num1;
q->num2 = p_num2;
q->result = p_result;
q->next = NULL;
p->next = q;
}
TS* GetListDataP(TS* head, int p_id)
{
TS* p = head;
while(p !=NULL)
{
if(p->id == p_id)
{
return p;
}
p = p->next;
}
return NULL;
}
char* PrintfList(TS* head) {
long res = 0;
char *str_res = malloc(sizeof(char)*17);
TS* ts = head;
while (ts != NULL)
{
res = res + ts->result;
//printf("id : %3d, num1 : %4ld, num2: %4ld, result : %ld, next : %o\n",ts->id,ts->num1,ts->num2,ts->result,ts->next);
ts = ts->next;
}
//printf("res : %ld\n",res);
//printf("+++++++++++++++++++++\n");
sprintf(str_res, "%lld", res);
return str_res;
}
(4)编译
gcc -o /opt/libtest.so -fPIC -shared multithreading_v2.c
(5)建C外部函数(此函数仅支持X86平台)
CREATE OR REPLACE FUNCTION multipthread_sum(pthread_num int,num integer)
RETURN varchar EXTERNAL '/opt/libtest.so' MULTITHREADING USING CS;
函数介绍:
pthread_num :表示线程数。
num :需要累加到的值,0加到num。
(6)建Plsql自定义函数–实现同样的功能
create or replace function psql_sum(num integer)
RETURN bigint
as
declare
i integer;
sum bigint:= 0;
begin
for i in 1..num loop
sum := sum + i;
end loop;
return sum;
end;
/
(7)效率对比
更多推荐
已为社区贡献2条内容
所有评论(0)