简体   繁体   English

如何改进算法的运行时间?

[英]How do I improve the runtime of my algorithm?

The aim is given a file, with the 1st line as the number of lines available, find how many pair of lines are permutations of each other.目的是给一个文件,第一行是可用的行数,找出有多少对行是彼此排列的。 Example would be that AABA is a permutation of BAAA.例如,AABA 是 BAAA 的排列。 The code is written in java.代码是用java写的。 This is my current code:这是我当前的代码:

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]));
    }
}

I would like to know if there is any way to improve the time efficiency of the program?我想知道有没有什么办法可以提高程序的时间效率? It is part of my class contest and some people have managed runtimes of around 25% faster.这是我的课堂竞赛的一部分,有些人管理的运行时速度提高了约 25%。 I tried different array sizes and this seem to work the best.我尝试了不同的数组大小,这似乎效果最好。

Multiply arr_size by 4. You need a lot of free slots to make open addressing efficient, and depending on what size is you may not be getting very many right now.arr_size乘以 4。您需要大量空闲插槽才能使开放寻址高效,并且根据size ,您现在可能不会获得太多空间。

Specify a larger buffer size on your buffered reader to reduce the I/O count.在缓冲读取器上指定更大的缓冲区大小以减少 I/O 计数。 32768 would be reasonable. 32768 是合理的。

Then work on efficiency in Data Both the hashing and comparison operations need to iterate through all 128 possible byte values, which is unnecessary.然后在Data提高效率 哈希和比较操作都需要遍历所有 128 个可能的字节值,这是不必要的。

Are you sure your code even gets the correct answer?你确定你的代码能得到正确的答案吗? It doesn't seem likely.似乎不太可能。

The easiest way to determine if two strings are permutations of each other is to sort the strings and compare them.确定两个字符串是否相互排列的最简单方法是对字符串进行排序并比较它们。 With that in mind, an easier and faster way to code this up would be to use a Map .考虑到这一点,一种更简单、更快捷的编码方法是使用Map Something like this:像这样的东西:

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

There are other ways to do this, but that's the easiest way I know of, and probably the fastest.还有其他方法可以做到这一点,但这是我所知道的最简单的方法,可能也是最快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM