采访编码Java排序

Kis*_*ran 10 java

编写一个java程序来读取文件中的输入,然后对每个单词中的字符进行排序.完成后,按升序对所有生成的单词进行排序,最后跟随文件中数值的总和.

  • 处理数据时删除特殊字符并停用单词
  • 测量执行代码所需的时间

让我们说文件的内容是:Sachin Tendulkar获得18111 ODI运行和14692测试运行.

输出:achins adeklnrtu adn cdeors dio estt nrsu nrsu 32803

拍摄时间:3毫秒

我的代码需要15毫秒才能执行.....

请建议我解决这个问题的任何快速方法...........

码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class Sorting {

    public static void main(String[] ags)throws Exception
    {
        long st=System.currentTimeMillis();
        int v=0;
        List ls=new ArrayList();
        //To read data from file
        BufferedReader in=new BufferedReader(
                 new FileReader("D:\\Bhive\\File.txt"));
        String read=in.readLine().toLowerCase();
        //Spliting the string based on spaces
        String[] sp=read.replaceAll("\\.","").split(" ");
        for(int i=0;i<sp.length;i++)
        {
            //Check for the array if it matches number
            if(sp[i].matches("(\\d+)"))
                //Adding the numbers
                v+=Integer.parseInt(sp[i]);
            else
            {
                //sorting the characters
                char[] c=sp[i].toCharArray();
                Arrays.sort(c);
                String r=new String(c);
                //Adding the resulting word into list
                ls.add(r);
            }
        }
        //Sorting the resulting words in ascending order
        Collections.sort(ls);
        //Appending the number in the end of the list
        ls.add(v);
        //Displaying the string using Iteartor
        Iterator it=ls.iterator();
        while(it.hasNext())
            System.out.print(it.next()+" ");
        long time=System.currentTimeMillis()-st;
        System.out.println("\n Time Taken:"+time);
    }
}
Run Code Online (Sandbox Code Playgroud)

Dhw*_*att 5

用于indexOf()从字符串中提取单词而不是split(" ").它提高了性能.

请参阅此主题:String中的StringTokenizer类与split方法的性能

此外,尝试增加输出的大小,复制粘贴线Sachin Tendulkar得分18111 ODI运行和14692测试运行.在文本文件中50,000次并测量性能.这样,当您尝试不同的优化时,您将能够看到相当大的时间差异.

编辑

测试了这段代码(使用过.indexOf())

        long st = System.currentTimeMillis();
        int v = 0;
        List ls = new ArrayList();
        // To read data from file
        BufferedReader in = new BufferedReader(new FileReader("D:\\File.txt"));
        String read = in.readLine().toLowerCase();
        read.replaceAll("\\.", "");
        int pos = 0, end;
        while ((end = read.indexOf(' ', pos)) >= 0) {
            String curString = read.substring(pos,end);
            pos = end + 1;
        // Check for the array if it matches number
            try {
                // Adding the numbers
                v += Integer.parseInt(curString);
            }
            catch (NumberFormatException e) {
                // sorting the characters
                char[] c = curString.toCharArray();
                Arrays.sort(c);
                String r = new String(c);
                // Adding the resulting word into TreeSet
                ls.add(r);
            }
        }
        //sorting the list
        Collections.sort(ls);
        //adding the number
        list.add(v);
        // Displaying the string using Iteartor 
        Iterator<String> it = ls.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        long time = System.currentTimeMillis() - st;
        System.out.println("\n Time Taken: " + time + " ms");
Run Code Online (Sandbox Code Playgroud)

在文件中使用1行的性能
您的代码:3 ms
我的代码:2 ms

在文件中使用50K行的性能
您的代码:45 ms
我的代码:32 ms

如您所见,当输入大小增加时,差异很大.请在您的机器上测试并分享结果.


nes*_*983 3

我唯一看到的是:以下行不必要地昂贵:

   System.out.print(it.next()+" ");
Run Code Online (Sandbox Code Playgroud)

这是因为打印效率低下,因为所有的冲洗都在进行。相反,使用字符串构建器构建整个字符串,然后减少到一次 print 调用。