Is java.util.Scanner that slow?

后端 未结 7 1669
悲&欢浪女
悲&欢浪女 2021-02-07 08:17

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:

Sc         


        
7条回答
  •  逝去的感伤
    2021-02-07 09:06

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

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

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

    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.

    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.

提交回复
热议问题