爪哇 - 将字符串转换为加倍的最有效方法 [英] Java - Most efficient way to convert string to double

查看:78
本文介绍了爪哇 - 将字符串转换为加倍的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个文本文件中读取并保存每一行(用逗号分开)到一个数组。唯一的问题是,大部分的数组中的元素都在这里为两个元素是字符串值,双击。作为这样的结果,我不得不使阵列的String []数组。由于这个原因,每当我想在数组中的双值执行了一些公式,我得先分析他们作为一个双重价值。我从字面上运行这些方程的1000+迭代,所以我的code是不断解析字符串成一个双。这是正在放缓我的程序一个昂贵的方式​​。有没有更好的方法可以让我从字符串数组值转换为双精度值或是否有更好的方法保存来自文本文件中的行,当我应该采取?谢谢

Hi I am reading from a text file and saving each line (split by a comma) into an array. The only problem is that most of the elements in the array are double values where as two elements are strings. As a result of this I had to make the array a String[] array. Due to this, whenever I want to perform some equations on the double values in the array, I have to first parse them as a double value. I am literally running 1000+ iterations of these equations, therefore my code is constantly parsing the strings into a double. This is a costly way which is slowing down my program. Is there a better way I can convert the values from the string array to double values or is there a better approach I should take when saving the lines from the text file? Thanks

下面是该阵列中的一个看起来像我已经从文本文件中读取后:

Here is what one of the arrays looks like after I have read from the text file:

String[] details = {"24.9", "100.0", "19.2" , "82.0", "Harry", "Smith", "45.0"};

我现在需要乘以所述第一2个元素,并添加到第3,第4和第7的元素的总和。换句话说,我只使用数字元素(被ofcourse保存为字符串)

I now need to multiply the first 2 elements and add that to the sum of the 3rd, 4th and 7th elements. In other words I am only using the numerical elements (that are ofcourse saved as strings)

double score = (Double.parseDouble(details[0]) * Double.parseDouble(details[1])) + Double.parseDouble(details[2]) + Double.parseDouble(details[3]) + Double.parseDouble(details[6]);

我要在文本文件(1000 +行)每一行做到这一点。由于这个结果我的程序运行速度非常慢。有没有更好的办法,我可以将字符串值转换为双?还是有更好的办法,我应该去了解它们存储在首位?

I have to do this for every single line in the text file (1000+ lines). As a result of this my program is running very slowly. Is there a better way I can convert the string values into a double? or is there a better way I should go about storing them in the first place?

编辑:我已经使用Profiler来检查code的哪一部分是最慢的,这的确是code,我在上面

I have used profiler to check which part of the code is the slowest and it is indeed the code that I have shown above

推荐答案

下面是产生像你描述这10000线长一输入文件,然后在回读,做您发布的计算和打印结果的一个例子到标准输出。我为了得到最糟糕的读取性能读取文件时,明确禁止任何缓冲。我也没有做任何缓存可言,正如其他人建议。整个过程,包括生成文件,做计算,打印结果,始终需要大约520-550毫秒。这是很难的慢,除非你重复成百上千的文件这个同样的过程。如果从此看到完全不同的表现,那么也许这是一个硬件问题。发生故障的硬盘可以下降读取性能到几乎为零。

Here's an example of generating an input file like the one you describe that's 10000 lines long, then reading it back in and doing the calculation you posted and printing the result to stdout. I specifically disable any buffering when reading the file in order to get the worst possible read performance. I'm also not doing any caching at all, as others have suggested. The entire process, including generating the file, doing the calculation, and printing the results, consistently takes around 520-550 ms. That's hardly "slow", unless you're repeating this same process for hundreds or thousands of files. If you see drastically different performance from this, then maybe it's a hardware problem. A failing hard disk can drop read performance to nearly nothing.

import java.io.*;
import java.util.Random;

public class ReadingDoublesFromFileEfficiency {
    private static Random random = new Random();

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        String filePath = createInputFile();
        BufferedReader reader = new BufferedReader(new FileReader(filePath), 1);
        String line;
        while ((line = reader.readLine()) != null) {
            String[] details = line.split(",");
            double score = (Double.parseDouble(details[0]) * Double.parseDouble(details[1])) + Double.parseDouble(details[2]) + Double.parseDouble(details[3]) + Double.parseDouble(details[6]);
            System.out.println(score);
        }
        reader.close();
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("Took " + elapsed + " ms");
    }

    private static String createInputFile() throws IOException {
        File file = File.createTempFile("testbed", null);
        PrintWriter writer = new PrintWriter(new FileWriter(file));
        for (int i = 0; i < 10000; i++) {
            writer.println(randomLine());
        }
        writer.close();
        return file.getAbsolutePath();
    }

    private static String randomLine() {
        return String.format("%f,%f,%f,%f,%s,%s,%f",
                score(), score(), score(), score(), name(), name(), score());
    }

    private static String name() {
        String name = "";
        for (int i = 0; i < 10; i++) {
            name += (char) (random.nextInt(26) + 97);
        }
        return name;
    }

    private static double score() {
        return random.nextDouble() * 100;
    }
}

这篇关于爪哇 - 将字符串转换为加倍的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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