--pgsql的实现
CREATE OR REPLACE FUNCTION pg_test1(a double precision, b double precision, c int)
RETURNS double precision
AS $$DECLARE
s DECIMAL := 0;
dx DECIMAL := (b-a) / c;
i int :=0;
begin
for i in 0 .. c-1 loop
s = s + power(2.71828182846, -1 power(a+idx, 2));
end loop;
return s*dx;
end$$ LANGUAGE plpgsql;
--plpython的实现,没有使用numba的jit
CREATE OR REPLACE FUNCTION py_test1(a double precision, b double precision, c int)
RETURNS double precision
AS $$
def integrate_f(a, b, N):
s = 0
dx = (b-a) /N
for i in range(N):
s += 2.71828182846*(-(a+idx)*2)
return sdx
return integrate_f(args[0], args[1], args[2])
$$ LANGUAGE plpython3u;
--plpython的实现,使用numba的jit
CREATE OR REPLACE FUNCTION py_numba_test1(a double precision, b double precision, c int)
RETURNS double precision
AS $$
需要使用pip安装,可能需要在管理员权限。
pip install numba
from numba import jit
@jit
def integrate_f(a, b, N):
s = 0
dx = (b-a) /N
for i in range(N):
s += 2.71828182846*(-(a+idx)*2)
return sdx
return integrate_f(args[0], args[1], args[2])
$$ LANGUAGE plpython3u;
CREATE or replace FUNCTION lua_test1(a double precision, b double precision, c int)
RETURNS double precision AS $$
local s = 0
local dx = (b - a) / c
local i=0
for i = 0, c-1 do
s = s + math.pow(2.71828182846, -1 math.pow(a + i dx, 2));
end
return s * dx
$$ LANGUAGE pllua;
SELECT lua_test1(1, 10, 100000000); //3.88秒
--在同一Windows 10 64位电脑上
--使用pl/pgsql执行时间:
-- 如果第3个参数值小些与python实现的显示结果是一样,说明算法没有问题的。
-- 如果第3个参数值100000000计算非常耗时,等了8分钟还没有算出来,不知道需要多久。
--使用CPython执行时间:54.625 s
--使用CPython+numba扩展的jit执行时间:5.61 s
--使用CPython+numba扩展的jit执行时间:5.61 s
--使用pl/c实现执行时间:11.781 s
include "postgres.h" //包含基本的类型与XXXGetDatum(XXX类型的值或引用)等方法。
include "fmgr.h" //C编写的数据库函数管理,开发新数的据库函数使用它得到支持,并支持调用内置的数据库函数。
include
extern double run(double a, double b, int c) {
double s = 0, dx = (b - a) / c;
for (int i = 0; i < c - 1; i++)
s += pow(2.71828182846, -1 * pow(a + i * dx, 2));
return s * dx;
}
PGDLLEXPORT Datum test(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(test);
Datum test(PG_FUNCTION_ARGS)
{
float8 a = PG_GETARG_FLOAT8(0);
float8 b = PG_GETARG_FLOAT8(1);
int c = PG_GETARG_INT32(2);
PG_RETURN_FLOAT8(run(a, b, c));
}
/*
CREATE or replace FUNCTION test(DOUBLE precision ,DOUBLE precision,int) RETURNS DOUBLE precision
AS 'Extension'
LANGUAGE C;
--pgsql的实现 CREATE OR REPLACE FUNCTION pg_test1(a double precision, b double precision, c int) RETURNS double precision AS $$DECLARE s DECIMAL := 0; dx DECIMAL := (b-a) / c; i int :=0; begin for i in 0 .. c-1 loop s = s + power(2.71828182846, -1 power(a+idx, 2)); end loop; return s*dx;
end$$ LANGUAGE plpgsql;
--plpython的实现,没有使用numba的jit CREATE OR REPLACE FUNCTION py_test1(a double precision, b double precision, c int) RETURNS double precision AS $$
def integrate_f(a, b, N): s = 0 dx = (b-a) /N for i in range(N): s += 2.71828182846*(-(a+idx)*2) return sdx
return integrate_f(args[0], args[1], args[2]) $$ LANGUAGE plpython3u;
--plpython的实现,使用numba的jit CREATE OR REPLACE FUNCTION py_numba_test1(a double precision, b double precision, c int) RETURNS double precision AS $$
需要使用pip安装,可能需要在管理员权限。
pip install numba
from numba import jit
@jit def integrate_f(a, b, N): s = 0 dx = (b-a) /N for i in range(N): s += 2.71828182846*(-(a+idx)*2) return sdx
return integrate_f(args[0], args[1], args[2]) $$ LANGUAGE plpython3u;
CREATE or replace FUNCTION lua_test1(a double precision, b double precision, c int) RETURNS double precision AS $$ local s = 0 local dx = (b - a) / c local i=0 for i = 0, c-1 do s = s + math.pow(2.71828182846, -1 math.pow(a + i dx, 2)); end return s * dx $$ LANGUAGE pllua;
SELECT lua_test1(1, 10, 100000000); //3.88秒
--在同一Windows 10 64位电脑上 --使用pl/pgsql执行时间: -- 如果第3个参数值小些与python实现的显示结果是一样,说明算法没有问题的。 -- 如果第3个参数值100000000计算非常耗时,等了8分钟还没有算出来,不知道需要多久。 --使用CPython执行时间:54.625 s --使用CPython+numba扩展的jit执行时间:5.61 s
--使用CPython+numba扩展的jit执行时间:5.61 s
--使用pl/c实现执行时间:11.781 s
select pg_test1(1.0, 10.0, 100000000); select py_test1(1.0, 10.0, 100000000); select py_numba_test1(1.0, 10.0, 100000000); select test(1.0, 10.0, 100000000); //pl/c实现,看test-power.c。 执行时间:11.781 sec --execution time: 11.781 sec; total time: 11.797 sec
SELECT lua_test1(1, 10, 100000000); //3.88秒 --execution time: 3.88秒
power.c:
include "postgres.h" //包含基本的类型与XXXGetDatum(XXX类型的值或引用)等方法。
include "fmgr.h" //C编写的数据库函数管理,开发新数的据库函数使用它得到支持,并支持调用内置的数据库函数。
include
extern double run(double a, double b, int c) { double s = 0, dx = (b - a) / c;
}
PGDLLEXPORT Datum test(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(test); Datum test(PG_FUNCTION_ARGS) { float8 a = PG_GETARG_FLOAT8(0); float8 b = PG_GETARG_FLOAT8(1); int c = PG_GETARG_INT32(2);
}
/* CREATE or replace FUNCTION test(DOUBLE precision ,DOUBLE precision,int) RETURNS DOUBLE precision AS 'Extension' LANGUAGE C;
select test(1.0, 10.0, 100000000); --execution time: 11.781 sec; total time: 11.797 sec
*/