DolphinDB database 从1.01版本开始支持即时编译JIT(https://github.com/dolphindb/... )。JIT又译及时编译或实时编译,是动态编译的一种形式,可提高程序运行效率。上面函数可写成JIT函数如下:
@jit def avg_price(price, amount){ hold = 0.0; cost = 0.0; avgPrice = 0.0; n = size(price) avgPrices = array(DOUBLE, n, n, 0) for ( i in 0..(n-1)) { hold =hold + amount[i]; if ( amount[i] > 0 ) { cost = cost + amount[i] * price[i]; if(hold > 0) { avgPrice = cost/hold; }else{ avgPrice = 0.0; } } else { cost += amount[i] * avgPrice;; } avgPrices[i]=avgPrice } return avgPrices; }
对上述函数进行测试,用JIT版本比非JIT大约快200倍。