简体   繁体   中英

Ironpython code taking too long to execute?

I have a piece of code written in Iron Python that reads data from table present in SpotFire and serialize in JSON object. It is taking too long to get executed. Please provide alternates to it.

import clr
import sys
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import DataValueCursor

rowCount = MyTable.RowCount
rows = IndexSet(rowCount,True)
cols = MyTable.Columns
MyTableData=[]

for r in rows:
 list={}
 item={}
 for c in cols:
  item[c.Name] = c.RowValues.GetFormattedValue(r)
  list['MyData']=item
 MyTableData.append(list)

json=JavaScriptSerializer(MaxJsonLength=sys.maxint).Serialize(MyTableData)

Your code will be faster if you don't call list['MyData']=item for every column. You only need to call it once.

You could also use list and dictionary comprehensions , instead of appending, or looking up keys for every value.

MyTableData = [{'MyData': {column.Name: column.RowValues.GetFormattedValue(row)
                           for column in cols}}
               for row in rows]

If column.RowValues is an expensive operation you may be better looping over columns, which isn't as neat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM