小編給大家分享一下如何使用fastcache,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)主營武侯網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),武侯h5成都微信小程序搭建,武侯網(wǎng)站營銷推廣歡迎武侯等地區(qū)企業(yè)咨詢
VnTrader 2.0版本有不少提速措施,其中l(wèi)ru_cache是提高回測速度一個(gè)利器,讓我用1.92為主的我很是羨慕。看說這個(gè)是python 3.5.2提供的功能,也就沒多想。
最近才發(fā)現(xiàn)其實(shí)有第三方在也支持python 2.7的版本,比如 functools32。還有一個(gè)用 C 語言實(shí)現(xiàn)的,更快的,同時(shí)兼容 Python2 和 Python3 的第三方模塊 fastcache 能夠?qū)崿F(xiàn)同樣的功能,這里就用fastcache。
很簡單,pip直接安裝就可以。
pip install fastcache --upgrade
import fastcache fastcache.test()
不用cache時(shí)候,運(yùn)行時(shí)間 0.7994769 秒
from fastcache import clru_cache
def fib(n):
if n < 2:
return n
return fib(n - 2) + fib(n - 1)
import time
dt0 = time.clock()
for i in range(30):
fib(i)
spreadtime = (time.clock() - dt0)
print ("運(yùn)行時(shí)間 %s 秒" %spreadtime)使用后,運(yùn)行時(shí)間 0.000185200000004 秒
@clru_cache(maxsize=999)
def fib_cache(n):
if n < 2:
return n
return fib_cache(n - 2) + fib_cache(n - 1)
import time
dt1 = time.clock()
for i in range(30):
fib_cache(i)
spreadtime = (time.clock() - dt1)
print ("運(yùn)行時(shí)間 %s 秒" %spreadtime)通常只在靜態(tài)方法使用lru_cache,這樣才具有意義, 雖然在類方法中也可以使用,但是這樣一個(gè)是有不同實(shí)例一般無法復(fù)用,而且會(huì)使得過期實(shí)例無法垃圾回收。這里新建一個(gè)靜態(tài)方法load_data,負(fù)責(zé)讀取數(shù)據(jù)庫數(shù)據(jù)。
@clru_cache(maxsize=9999)
def load_data(dbName, symbol, dataStartDate, strategyStartDate, dataEndDate, dataClass):
dbClient = pymongo.MongoClient(globalSetting['mongoHost'], globalSetting['mongoPort'])
collection = dbClient[dbName][symbol]
# 載入初始化需要用的數(shù)據(jù)
flt = {'datetime': {'$gte': dataStartDate,
'$lt': strategyStartDate}}
initCursor = collection.find(flt).sort('datetime')
initData = [] # 清空initData列表
for d in initCursor:
data = dataClass()
data.__dict__ = d
initData.append(data)
# 載入回測數(shù)據(jù)
if not dataEndDate:
flt = {'datetime': {'$gte': strategyStartDate}} # 數(shù)據(jù)過濾條件
else:
flt = {'datetime': {'$gte': strategyStartDate,
'$lte': dataEndDate}}
BackData = []
dbCursor = collection.find(flt).sort('datetime')
for dc in dbCursor:
data = dataClass()
data.__dict__ = dc
BackData.append(data)
count = len(initData) + len(BackData)
return initData, BackData, count修改已有方法BacktestingEngine.loadHistoryData; 改為使用剛剛創(chuàng)建靜態(tài)方法
def loadHistoryData(self): """載入歷史數(shù)據(jù)""" self.output(u'開始載入數(shù)據(jù)') # 首先根據(jù)回測模式,確認(rèn)要使用的數(shù)據(jù)類 # load_data(dbName, symbol, dataStartDate, strategyStartDate, dataEndDate, dataClass) if self.mode == self.BAR_MODE: dataClass = VtBarData func = self.newBar self.initData,self.BackTestData, count = load_data(self.dbName,self.symbol, self.dataStartDate, self.strategyStartDate, self.dataEndDate, dataClass) else: dataClass = VtTickData func = self.newTick self.initData, self.BackTestData, count = load_data(self.dbName, self.symbol, self.dataStartDate, self.strategyStartDate, self.dataEndDate, dataClass) # 載入初始化需要用的數(shù)據(jù) if self.hdsClient: initCursor = self.hdsClient.loadHistoryData(self.dbName, self.symbol, self.dataStartDate, self.strategyStartDate) # 將數(shù)據(jù)從查詢指針中讀取出,并生成列表 self.initData = [] # 清空initData列表 for d in initCursor: data = dataClass() data.__dict__ = d self.initData.append(data) # 載入回測數(shù)據(jù) self.dbCursor = self.hdsClient.loadHistoryData(self.dbName, self.symbol, self.strategyStartDate, self.dataEndDate) for dc in self.dbCursor: data = dataClass() data.__dict__ = dc self.BackTestData.append(data) self.output(u'載入完成,數(shù)據(jù)量:%s' % count)
修改 BacktestingEngine.runBacktesting; 改為使用換成的BackTestData 隊(duì)列,而不是數(shù)據(jù)庫指針。
def runBacktesting(self): """運(yùn)行回測""" # 載入歷史數(shù)據(jù) self.loadHistoryData() # 首先根據(jù)回測模式,確認(rèn)要使用的數(shù)據(jù)類 if self.mode == self.BAR_MODE: dataClass = VtBarData func = self.newBar else: dataClass = VtTickData func = self.newTick self.output(u'開始回測') self.strategy.onInit() self.strategy.inited = True self.output(u'策略初始化完成') self.strategy.trading = True self.strategy.onStart() self.output(u'策略啟動(dòng)完成') self.output(u'開始回放數(shù)據(jù)') for d in self.BackTestData: func(d) self.output(u'數(shù)據(jù)回放結(jié)束')
沒有使用之前,優(yōu)化約為100組參數(shù)約為運(yùn)行時(shí)間 323.0888239 秒,使用cache優(yōu)化后運(yùn)行時(shí)間 190.762839 秒
以上是“如何使用fastcache”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:如何使用fastcache
網(wǎng)站URL:http://chinadenli.net/article4/gdjeoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、域名注冊、App設(shè)計(jì)、小程序開發(fā)、網(wǎng)站營銷、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)