Skip to content

Commit

Permalink
Reduced the cost of 'getTrades'
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Apr 28, 2015
1 parent 221d82b commit 4fbfb52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014

v6.18.6 Apr 27 2015
. (kfsone) Fixed #225 'Generator already running'
. (kfsone) Performance improvement: Reduced the cost of "getTrades"

v6.18.5 Apr 26 2015
. (kfsone) Fixed #224 run -vv and above was raising an exception
Expand Down
37 changes: 18 additions & 19 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,25 +667,24 @@ def getTrades(self, srcStation, dstStation, srcSelling=None):
itemIdx = self.tdb.itemByID
minGainCr = max(1, self.tdenv.minGainPerTon or 1)
maxGainCr = max(minGainCr, self.tdenv.maxGainPerTon or sys.maxsize)
for buy in dstBuying:
buyItemID = buy[0]
for sell in srcSelling:
sellItemID = sell[0]
if sellItemID == buyItemID:
buyCr, sellCr = buy[1], sell[1]
if sellCr + minGainCr > buyCr:
continue
if sellCr + maxGainCr < buyCr:
continue
trading.append(Trade(
itemIdx[buyItemID],
buyItemID,
sellCr, buyCr - sellCr,
sell[2], sell[3],
buy[2], buy[3],
sell[4], buy[4],
))
break # from srcSelling
buyIndex = {buy[0]: buy for buy in dstBuying}
getBuy = buyIndex.get
for sell in srcSelling: # should be the smaller list
itmID = sell[0]
buy = getBuy(itmID, None)
if buy:
buyCr, sellCr = buy[1], sell[1]
if sellCr + minGainCr > buyCr:
continue
if sellCr + maxGainCr < buyCr:
continue
trading.append(Trade(
itemIdx[itmID], itmID,
sellCr, buyCr - sellCr,
sell[2], sell[3],
buy[2], buy[3],
sell[4], buy[4],
))

# SORT BY profit DESC, cost
# So if two items have the same profit, the cheapest will come first.
Expand Down

0 comments on commit 4fbfb52

Please sign in to comment.