简体   繁体   English

我如何优化此代码以看起来更高效?

[英]how i can optimize this code to look more performant?

I have this Java code that splits the age of users into 8 groups: 我有这段Java代码,将用户年龄分为8组:

private int[] A0, A1, A2, A3, A4, A5, A6, A7, A8;

    public Grouping() {
        super();

        for (int i = 5; i < 10; i++)
            A0[i] = i;

        for (int i = 10; i < 20; i++)
            A1[i] = i;

        for (int i = 20; i < 30; i++)
            A2[i] = i;

        for (int i = 30; i < 40; i++)
            A3[i] = i;

        for (int i = 40; i < 50; i++)
            A4[i] = i;

        for (int i = 50; i < 60; i++)
            A5[i] = i;

        for (int i = 60; i < 70; i++)
            A6[i] = i;

        for (int i = 70; i < 80; i++)
            A7[i] = i;

        for (int i = 10; i < 20; i++)
            A8[i] = i;

    }

Now I would like to optimize this code. 现在,我想优化此代码。 Are there any ideas? 有什么想法吗?

You've said you want it "shorter." 您已经说过要“缩短”。 Here's a shorter version, except I had no idea what you were trying to do with A8 , which is redundant with A1 , and so have left that off: 这是一个简短的版本, 除了我不知道您要使用A8做什么,这对A1是多余的,因此省略了它:

private int[][] groups; // I assume you initialize `A0` and so on somewhere you haven't shown; initialize this there

public Grouping() {
    int i, groupIndex, group;

    super();

    groupIndex = 0;
    group = groups[0];
    for (i = 5; i < 80; ++i) {
        if (i % 10 == 0) {
            ++groupIndex;
            group = groups[groupIndex];
        }
        group[i] = i;
    }
}

Note though that this is still fairly weird code because it does what your original code does and leaves all sorts of entries in the arrays at their default value ( 0 ). 请注意,尽管这仍然是很奇怪的代码,因为它执行原始代码所执行的操作,并将数组中的所有条目保留为默认值( 0 )。 For instance, in your code, A0 's indexes 0 through 4 never get assigned to, and that's true of groups[0] 's 0 through 4 above as well. 例如,在您的代码中,永远不会将A0的索引04分配给它,上面的groups[0]04也是如此。 (And the subsequent ones have larger gaps.) (并且后续的差距更大。)

Your title , though, says you want it more "performant." 不过, 标题表示您希望它更“出色”。 "performant" != "shorter" Your code probably performs just fine, likely an imperceptibly amount better than mine above as it doesn't need the % operation and the if inside the loop. “ performant”!=“更短”您的代码可能执行得很好,可能比上面的代码好很多,因为它不需要%操作和if循环内的代码。

Or here's a version implementing Seelenvirtuose's suggestion : 或者这是实现Seelenvirtuose的建议的版本:

private int[][] groups; // I assume you initialize `A0` and so on somewhere you haven't shown; initialize this there

public Grouping() {
    int i;

    super();

    for (i = 5; i < 80; ++i) {
        groups[i / 10][i] = i;
    }
}

...which works because the i / 10 bit is an integer division. ...之所以有效,是因为i / 10位是整数除法。

Create a group class which will hold age range and a counter to hold matches for that group. 创建一个分组类别,该分组类别将保留年龄范围和一个计数器以容纳该分组的匹配项。 Add group objects to a list and play around. 将组对象添加到列表中并播放。

public static void main(String[] args) {

    List<Group> lists = new ArrayList<Group>(10);

    lists.add(new Group(5, 10));
    lists.add(new Group(10, 20));
    lists.add(new Group(20, 30));
    lists.add(new Group(30, 40));

    int age[] = { 5, 10, 20, 30, 11, 22, 33 };

    for (int i = 0; i < age.length; i++)
        grouper: for (Group group : lists)
            if (group.validateAgeGroup(age[i]))
                break grouper;

    for (Group group : lists)
        System.out.println(group);
}

Group Class : age range and a counter: 小组课程:年龄段和计数器:

public class Group {

    int startAge, endAge, counter;

    public Group(int startAge, int endAge) {
        this.startAge = startAge;
        this.endAge = endAge;
    }

    public boolean validateAgeGroup(int age) {
        if (age >= startAge && age < endAge) {
            this.setCounter(1);
            return true;
        }
        return false;
    }

    public int getCounter() {
        return counter;
    }

    public int getEndAge() {
        return endAge;
    }

    public int getStartAge() {
        return startAge;
    }

    public void setCounter(int counter) {
        this.counter += counter;
    }

    public void setEndAge(int endAge) {
        this.endAge = endAge;
    }

    public void setStartAge(int startAge) {
        this.startAge = startAge;
    }

    @Override
    public String toString() {
        return "Group [startAge=" + getStartAge() + ", endAge=" + getEndAge()
                + ", counter=" + getCounter() + "]";
    }
}

I'd use java8 streams. 我会使用java8流。 See my example code: 参见我的示例代码:

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Grouping {
    public static void main(String[] args) {

        // create a stream from 5 (inclusive) to 80 (exclusive)
        IntStream.range(5, 80)

        // convert it to Integer objects
        .boxed()

        // group it by tens
        .collect(Collectors.groupingBy(i -> i / 10))

        // iterate over each group
        .entrySet().stream()

        // pretty format
        .map(entry -> "A"+entry.getKey()+"="+entry.getValue())

        // print to console
        .forEach(System.out::println);
    }
}

it outputs: 它输出:

A0=[5, 6, 7, 8, 9]
A1=[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
A2=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
A3=[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
A4=[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
A5=[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
A6=[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
A7=[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]

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

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