Ironpython代码执行时间太长?

时间:2023-01-03 04:02:36

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.

我有一段用Iron Python编写的代码,它从SpotFire中的表中读取数据并在JSON对象中序列化。被执行需要很长时间。请提供替代方案。

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)

1 个解决方案

#1


0  

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

如果不为每列调用list ['MyData'] = item,您的代码会更快。你只需要调用一次。

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.

如果column.RowValues是一个昂贵的操作,你可能更好地循环列,这不是很整洁。

#1


0  

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

如果不为每列调用list ['MyData'] = item,您的代码会更快。你只需要调用一次。

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.

如果column.RowValues是一个昂贵的操作,你可能更好地循环列,这不是很整洁。