是java.util.Scanner中的慢? [英] Is java.util.Scanner that slow?

查看:263
本文介绍了是java.util.Scanner中的慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Android应用程序我想用扫描仪类从一个文本文件中读取花车的列表(这是顶点的坐标列表对OpenGL)。精确code是:

In a Android application I want to use Scanner class to read a list of floats from a text file (it's a list of vertex coordinates for OpenGL). Exact code is:

Scanner in = new Scanner(new BufferedInputStream(getAssets().open("vertexes.off")));
final float[] vertexes = new float[nrVertexes];
for(int i=0;i<nrVertexFloats;i++){
    vertexes[i] = in.nextFloat();
}

这似乎然而,这是令人难以置信的速度慢(它花了30分钟时间读一读万花车!) - 作为测试的2.1模拟器。这是怎么回事? 我不记得扫描仪要慢,当我用它在PC上(说实话我以前从来没读过超过100个值)。抑或是别的东西,像从一个资产读取输入流?

It seems however that this is incredibly slow (it took 30 minutes to read 10,000 floats!) - as tested on the 2.1 emulator. What's going on? I don't remember Scanner to be that slow when I used it on the PC (truth be told I never read more than 100 values before). Or is it something else, like reading from an asset input stream?

感谢您的帮助!

推荐答案

不知道关于Android,但至少在JavaSE中,扫描仪是缓慢的。

Don't know about Android, but at least in JavaSE, Scanner is slow.

在内部,扫描程序UTF-8的转换,这是用浮漂的文件也没用。

Internally, Scanner does UTF-8 conversion, which is useless in a file with floats.

由于所有你想要做的就是从文件中读取浮动,你应该去与java.io包。

Since all you want to do is read floats from a file, you should go with the java.io package.

SPOJ 与I / O速度斗争的人。这是波兰的编程比赛现场非常困难的问题。它们的区别是,他们接受的编程语言比其他网站更广泛的阵列,并在他们的许多问题,输入是如此之大,如果你不写高效的I / O,你的程序会破灭的时间限制。

The guys on SPOJ struggle with I/O speed. It's is a Polish programming contest site with very hard problems. Their difference is that they accept a wider array of programming languages than other sites, and in many of their problems, the input is so large that if you don't write efficient I/O, your program will burst the time limit.

检查他们的论坛,例如,<一个href="http://www.spoj.pl/forum/viewtopic.php?f=43&t=4864&sid=9b0ab81b86c28738e83e86a43a59d7c6">here,一个自定义的解析器的想法。

Check their forums, for example, here, for an idea of a custom parser.

当然,我建议的的编写自己的浮点解析器,但如果你需要的速度,这仍然是一个解决方案。

Of course, I advise against writing your own float parser, but if you need speed, that's still a solution.

这篇关于是java.util.Scanner中的慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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