Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the performance of void programs

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to optimize the performance of void programs". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "how to optimize the performance of void programs" can help you solve the problem.

Our compiler has provided a good optimization mechanism, but there are still many details that the compiler can't optimize, or don't have the courage to optimize, because some radical optimizations are likely to go against the programmer's original intention.

/ / the first kind

Void twiddle1 (long * xp, long * yp) {

* xp + = * yp

* xp + = * yp

}

/ / the second kind

Void twiddle2 (long * xp, long * yp) {

* xp + = 2 * * yp

}

For example, in the above program, when we see the first way of writing, we may easily think of the second way of writing, but the compiler will not change it into this way. At first glance, there is no difference between them. Let's analyze the memory references. The first requires 3 memory references, namely, read\ * xp, read\ * yp, and write\ * xp;, while the second requires 6 memory references, that is, 2 reads\ * xp, 2 reads\ * yp, and 2 writes\ * xp. So the performance of the first is better than the second.

So why can't the compiler think of the second way when it sees the first one? Isn't that a simple rule? In fact, the above program has the case of xp = yp, that is, two pointers point to the same memory location.

/ / the first kind

The value stored at * xp + = * yp; / / xp multiplied by 2

The value stored at * xp + = * yp; / / xp multiplied by 2

/ / the second kind

The value stored at * xp + = 2 * * yp; / / xp multiplied by 3

As you can see, when they all point to the same piece of memory, the first method will quadruple the original value, while the second method will increase the original value three times, resulting in a different effect, and the compiler will take this situation as possible. so the compiler will not help us optimize the first kind of code, which needs to be maintained by the programmer himself.

Eliminate the inefficiency of the cycle

I believe that many people have written the following similar code, there seems to be nothing to optimize, the writing is very good.

Void lower1 (char * s) {

Long i

For (I = 0; I)

< strlen(s); i++){ if(s[i] >

='A'& & s [I] ='A'& & s [I] b [I]) {

Long t = a [I]

A [I] = b [I]

B [I] = t

}

}

}

/ / the second kind

Void minmax2 (long a [], long b [], long n) {

Long i

For (I = 0; I < n; iTunes +) {

Long min = a [I] < b [I]? A [i]: b [i]

Long max = a [I] < b [I]? B [i]: a [i]

A [I] = min

B [I] = max

}

} this is the end of the content about "how to optimize the performance of void programs". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report