简体   繁体   English

有人可以告诉我任何其他方法来使此代码更快吗?

[英]Could someone tell me any different way to make this code faster?

The code runs correctly and it does what it is supposed to do, but I was told I could make it faster by using Boolean expressions instead but would not really know where to insert them here. 该代码可以正确运行,并且可以执行预期的操作,但是有人告诉我可以通过使用布尔表达式来提高它的速度,但是我真的不知道在哪里将其插入。 The problem is: 问题是:

Given a sequence of n points with their coordinates, write a program remote, which calculates the value of the smallest remoteness of a point, which is outside the square. 给定一个由n个点组成的坐标序列,编写一个程序,该程序计算一个点的最小距离的值,该点位于正方形之外。 A point is outside the square, if it is neither inner to the square, nor belongs to square contour. 如果点既不在正方形内部,也不属于正方形轮廓,则该点位于正方形外部。 If there are no points outside the square, your program has to output 0. 如果正方形外没有任何点,则程序必须输出0。

Constraints: 限制条件:
1 ≤ n ≤ 10000 and 1 ≤ a ≤ 1000 ; 1≤n≤10000和1≤a≤1000;
Example: 例:

Input: 5 4 输入:5 4
1 2 1 2
4 6 4 6
-3 2 -3 2
-2 2 -2 2
4 -1 4 -1
Output: 5 输出:5

Could someone suggest me any technique to make the code more efficient? 有人可以建议我一些提高代码效率的技术吗?

int remote(int x, int y) {
    int z = abs(x) + abs(y);
    return z;
}   

int main() {

    int n, a;
    int x;
    int y;

    cin >> n >> a;

    int z=20001;
    for (int i = 1; i <= n; i++) {
        cin >> x >> y;
        if (x > a / 2 || y > a / 2) {
            if (z > remote(x, y)) {
                z = remote(x, y);
            }               
        }
    }    
    cout << z <<endl;

    return 0;

}

For one, you are calling remote twice (in some cases) needlessly. 例如,您不必要地拨打两次remote电话(在某些情况下)。 Consider using this: 考虑使用此:

#include <algorithm>

z = std::max(z, remote(x, y));

This will also shorten and clarify the code. 这还将缩短并阐明代码。


Also, it's possible the divisions are slow. 另外,划分可能很慢。 Try (after profiling!) replacing 尝试(分析后!)替换

x > a / 2 || y > a / 2

by 通过

(x << 1) > a || (y << 1) > a

Note @Donnie & others claims in the comments that compilers will do the latter optimization, and they are probably correct. 注意 @Donnie&others在注释中声称编译器将进行后一种优化,并且它们可能是正确的。

I would like to show you the timings on my machine: 我想向您展示我的机器上的计时:

Version 1: 版本1:

for (int i = 1; i <= n; i++) {
    cin >> x >> y;
   if (x > a / 2 || y > a / 2) {
        if (z > remote(x, y)) {
            z = remote(x, y);
        }               
    }
} 

Version 2: 版本2:

for (int i = 1; i <= n; i++) {
    cin >> x >> y;
/*    if (x > a / 2 || y > a / 2) {
        if (z > remote(x, y)) {
            z = remote(x, y);
        }               
    }
 */
} 

For n=10^5, compiled with -O3 both yield 60ms. 对于n = 10 ^ 5,用-O3编译都产生60ms。 Compiled without optimization: both 60ms. 无需优化即可编译:均为60ms。

First step for optimizing is to know where your program spends time. 优化的第一步是知道程序在哪里花费时间。 Reading/parsing the data is the bottle neck. 读取/解析数据是瓶颈。

You could speed up it a little bit by adding as first line to your main: 您可以通过在第一行中添加以下内容来加快速度:

ios_base::sync_with_stdio(false);

On my machine I'm down to 20ms. 在我的机器上,我只有20毫秒。

1) Assign a temporary value to the remote function: 1)为remote功能分配一个临时值:

if (x > a / 2 || y > a / 2)
{
    const int r = remote(x,y);
    if (z > r)
    {
        z = r;
    }
}

2) Replace the call to remote with the contents of remote , removing the overhead of a function call: 2)更换调用remote用的内容remote ,去除函数调用的开销:

if (x > a / 2 || y > a / 2)
{
    const int r = abs(x) + abs(y);
    if (z > r)
    {
        z = r;
    }
}

3) Replace a / 2 with a constant temporary variable: 3)用常量临时变量替换a / 2

const int midpoint = a >> 1;
if (x > midpoint || y > midpoint)

4) Change compiler optimization level to high - for speed. 4)将编译器优化级别更改为高-以提高速度。

5) The bottleneck is now in the input statement. 5)瓶颈现在在输入语句中。 Any gain by optimizing the remainder of the loop is wasted by the Input time. 通过优化循环的其余部分获得的任何增益都会浪费输入时间。 There is no more Return On Investment for further changes. 没有更多的投资回报率可作进一步的改变。

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

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