GWT中大数值数组的有效表示 [英] Efficient representation for large numeric arrays in GWT

查看:112
本文介绍了GWT中大数值数组的有效表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间序列课程,在一天中将持有100K-200K的值(基本上市场报价,均匀采样)。在java方面,最高效的表示是使用double [](而不是List)。我怀疑这种方法能够很好地映射到javasctipt。在Java方面,double []数组必须定期增长(即分配一个新数组并将旧数组复制新的)。因此,例如,类有一个方法:

  public void add(long time,double price)
{
...
if(_len == _prices.length)
expand();

_prices [_len ++] =价格;
...
}

private void expand()
{
final double [] newprices = new double [_prices.length + 1024];
System.arraycopy(_prices,0,newprices,0,_len);
_prices = newprices;



$ b现在javascript有一个不同的数组模型,并且允许通过超越最后一个索引,隐式调整分配。


GWT有一个JsArrayNumeric,它允许用户查看和操作JS数值数组。 JsArrayNative被认为是来自JS的返回签名,并且不能被实例化(是的,可以在JS方面甩掉一些有条件的JSNI使用,但很难看)。

问题在GWT中,我可以在java类中使用哪些数据结构以获得对JS表示的最佳映射?我正在寻找最具性能的方法:


  • 为时间序列添加价格(增加时间序列)

  • 访问时间序列(通常会按索引扫描其中的一部分)/ li>


例如,ArrayList< Double>映射到JavaScript中紧的东西?可能这有更好的映射,使用raw double []。

-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/core/client/JsArrayNumber.htmlrel =nofollow noreferrer> JSArrayNumber 将为您提供最高效的映射。 ArrayList将为您打包值,这将意味着每个值的对象包装。即使double []也会遵守Java初始值语义(确保每个元素都初始化为0.0)。 JSArrayNumber将直接映射到包含原始数字的JavaScript数组。



这里有一些陷阱,因为JavaScript数组可以为未初始化的索引返回undefined,并且指定给索引实际上可以改变长度。你将不得不考虑这些。我通常创建JsArrayNumber的子类并使用断言来验证使用。



另外,您不必使用JSNI来实例化JsArrayNumber:

  JSArrayNumber a = JavaSriptObject.createArray()。cast(); 


I have a timeseries class that, over the course of a day will hold 100K-200K values (basically market ticks, uniformly sampled). On the java side the most performant representation is to use double[] (as opposed to say List). I am doubtful that this approach maps well into javasctipt.

On the Java side, the double[] array must grow periodically (ie allocate a new array and copy the old into the new). So for instance, the class has a method like:

public void add (long time, double price)
{
    ...
    if (_len == _prices.length)
        expand ();

    _prices[_len++] = price;
    ...
}

private void expand ()
{
    final double[] newprices = new double [_prices.length + 1024];
    System.arraycopy (_prices, 0, newprices, 0, _len);
    _prices = newprices;
}

Now javascript has a different array model, and allows incremental expansion via indexing beyond the last index, adjusting allocation implicitly.

GWT has a JsArrayNumeric which allows one to see and manipulate JS numerical arrays. JsArrayNative is meant to be a return signature from JS though and cannot be instantiated (yes could whip up some conditional use of JSNI on the JS side, but ugly).

Question is, in GWT, what datastructures can I use in the java class to get the best mapping to the JS representation? I'm looking for the most performant approach for:

  • adding a price to the timeseries (growing the timeseries)
  • accessing the timeseries (will often scan a part of it by index)/li>

Does ArrayList<Double>, for instance, map to something tight in javascript? Might this have a better mapping that using raw double[].

解决方案

JSArrayNumber is going to give you the most efficient mapping. ArrayList will box values for you, which will mean a object wrapper for every value. Even double[] will honor the Java initial value semantics (ensuring that every element is initialized to 0.0). JSArrayNumber will map directly onto JavaScript array holding primitive numbers.

There are some trappings here since JavaScript arrays can return undefined for uninitialized indexes and assigning to an index can actually change the length. You will have to take these into account. I usually create subclass of JsArrayNumber and use assertions to validate use.

Also, you don't have to use JSNI to instantiate a JsArrayNumber:

JSArrayNumber a = JavaSriptObject.createArray().cast();

这篇关于GWT中大数值数组的有效表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆