【问题标题】:How do I improve the runtime of my algorithm?如何提高算法的运行时间?
【发布时间】:2020-07-07 02:27:42
【问题描述】:

目标给定一个文件,第一行作为可用行数,求有多少对行是相互排列的。例如,AABA 是 BAAA 的排列。代码是用java编写的。这是我当前的代码:

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

public class SpeedDemon {

    public class Data{
        byte[] dataValues;
        byte duplicate=1;
        int hashcode;
        public Data(byte[] input) {
            dataValues= new byte[128];
            for (byte x : input) {
                if (x==10){
                    break;
                }
                dataValues[x]++;
            }
            hashcode = Arrays.hashCode(dataValues);
        }
        public boolean equal(Data o){
            return this.hashcode==o.hashcode&&Arrays.equals(o.dataValues, this.dataValues);
        }
    }
    public int processData(String fileName){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            int size = Integer.parseInt(reader.readLine());
            int arr_size = 2;
            while (arr_size < size) {
                arr_size *= 2;
            }
            Data[] map = new Data[arr_size];
            int z = 0;
            Data data;
            int j;
            for (int i = 0; i < size; i++) {
                data = new Data(reader.readLine().getBytes());
                j = data.hashcode;
                j ^= (j >>> 16);
                j &= (arr_size - 1);
                while (true) {
                    if (map[j] == null) {
                        map[j] = data;
                        break;
                    } else {
                        if (map[j].equal(data)) {
                            z += map[j].duplicate++;
                            break;
                        } else {
                            j = j == arr_size - 1 ? 0 : j + 1;
                        }
                    }
                }
            }
            return z;
        }catch(Exception ex){ }
        return 0;
    }
    public static void main(String[] args) {
        System.out.println(new SpeedDemon().processData(args[0]));
    }
}

我想知道有没有什么办法可以提高程序的时间效率?这是我课堂竞赛的一部分,有些人管理的运行时间快了 25% 左右。我尝试了不同的数组大小,这似乎效果最好。

【问题讨论】:

  • 我只是好奇,你的输入文件有多大?您如何测试性能,之前和之后的常规时间戳或一些有效的基准?
  • 我主要用这里的提交链接进行测试:speed-demon.herokuapp.com

标签: java algorithm hash


【解决方案1】:

arr_size 乘以 4。您需要大量空闲槽来提高开放寻址效率,并且取决于 size 是什么,您现在可能不会得到很多。

在缓冲读取器上指定更大的缓冲区大小以减少 I/O 计数。 32768 是合理的。

然后在Data中提高效率,哈希和比较操作都需要遍历所有128个可能的字节值,这是不必要的。

【讨论】:

    【解决方案2】:

    您确定您的代码甚至可以得到正确答案吗?好像不太可能。

    确定两个字符串是否相互排列的最简单方法是对字符串进行排序并进行比较。考虑到这一点,一种更简单、更快捷的编码方式是使用Map。像这样的:

    Create a new Map where the key and value are both strings
    for each line of the file
        s = read string from file
        sortedString = sort(s) // sort characters in the string
        if (map.contains(sortedString))
            you found a duplicate
        else
            map.insert(sortedString, string) // the key is the sorted string
    end for
    

    还有其他方法可以做到这一点,但这是我所知道的最简单的方法,而且可能是最快的。

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      相关资源
      最近更新 更多