In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to talk about. NET Micro Framework performance optimization, the content is concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.
. NET Micro Framework's tailorability, high performance, and natural integration with hardware all make it look promising. Of course, it's still young, and with the release of SDK v3.0, it still has a long way to go.
Let's not talk nonsense, let's talk about the experience I have used in the past few months to optimize the performance of. NET Micro Framework applications.
1. Minimize method calls!
Too many method calls have a huge impact on performance, so all optimizations are based on this premise.
2. Avoid attributes whenever possible and use common domains instead.
Because the editor adds access methods to getters and setters for each attribute at compile time, this is to be avoided based on the *** clause.
Of course, it was not to say that he completely avoided using attributes. After all, attributes were sometimes very convenient and necessary.
For example, the following example uses attributes:
public class Test { public string Name { get; set; } }
It needs to be changed to this:
public class Test { public string Name; }
3. Initialize variables only in constructors.
This one is easy to understand, look at the following example:
public class Test { private string name = "Test String"; private DateTime date = DateTime.Now; private int score, counter; public Test() { score = 0; counter = 0; } }
The initialization actually takes place twice, once when declaring variables and once when calling constructors. Following the *** principle, we want to minimize method calls, and the probability of using constructors is very high, so we need to focus all the initialization work on constructors here.
4. Call lock only where necessary.
For a semi-real-time system like MF, the cost of lock is much higher than we thought. In. NET inside may not feel out, but to MF this young man's hand feeling is very obvious, perhaps this one we already know, when I repeat it.
Consider this example:
public class Test { private ArrayList objs; public void SomeMethod(object o) { for(int i = 0; i
< 100; i++) { if (objs.Contains(o)) { lock(objs.SyncRoot) { objs.Remove(o); } } } } } 在一个循环里面增删一个集合,由于是多线程访问,所以在操作之前加了锁。之所以在循环内部加锁,理由可能是想尽可能的减少lock的访问次数,只有满足那个if条件的时候才会被调用。 实际上,这个想法错了,无论如何,这里的lock都会被调用很多次,这些开销加起来就会对性能造成很大的影响。 把代码改成这样就会好很多: public class Test { private ArrayList objs; public void SomeMethod(object o) { lock(objs.SyncRoot) { for(int i = 0; i < 100; i++) { if (objs.Contains(o)) { objs.Remove(o); } } } } } 5. 保证每个时间只有一个线程在运行。 用惯了.NET,来到MF世界***个不适应就是它的多线程太慢了,如果同时打开两个线程工作,那么整个程序的效率都会受到极大的影响。 拿电子地图软件来做例子,主线程负责更新UI,工作线程负责在后台取得地图块。这样的设计本身无可厚非也是合理的,但***我们发现性能实在太差了。 后来更改成为当用户在操作UI的时候,工作线程全都暂停,只有检测到用户没有任何操作的时候才进行工作。 要实现这一点,就要求程序在设计的时候就考虑到工作线程的可暂停性。 6. 尽可能少的并且在最小的范围内调用Invalidate()方法。 很多人在重画UI之后都会习惯性的调用顶层元素的Invalidate()方法来更新所有子控件,因为这样是最快捷的。可很多时候我们忽略了一点,Invalidate()这个方法可能在背后已经被调用过很多次了。 比如,有的控件会在得到焦点的时候调用这个方法,有的控件会在出发用户事件的时候自动调用这个方法。因为这些都是在背后发生的,我们可能并不知情,所以在完成我们自己控件的绘制之后通常会调用parent的Invalide来更新整个布局,这样就会在不知不觉之间导致了不必要的重画产生。 要避免这个问题也很简单,一则仔细观察,二则用Refactor!去阅读一下别人的代码。 7. 尽可能少的使用图片资源。 因为MF本身的数据吞吐量很小,如果载入过多图片资源的话,轻则程序运行效率变低,重则出现内存溢出。所以这里的原则我们要参照网页的设计原则,例如一个按钮图片,把它切割成几个小块,利用重复贴图来完成中间部分,而不要直接使用一整张图片。 同样在制作高亮的时候可以通过改变图片透明度或者在图片上面加一层透明矩形来实现。 8. 仅导入必要的字体资源。 这一点和上一条的理由是一样的,都是减少运行期间的数据吞吐量。对于英文来说还好,本来就不大,对于中文来说就很重要了,因为中文字体动辄就是几百k上兆,如果全部导入的话简直就是灾难。 ***就是程序用到多少就导入多少,实在没办法,就把生僻字全部剔出吧。 9. 窗体***用完就是立即关闭。 这一点对于窗体很多的应用程序非常重要!在.NET的世界里,打开一个主窗体,然后在主窗体里面创建子窗体的做法非常常见。但这可能会成为你的MF程序运行效率***的隐性杀手。 例如 主窗体 ->Product List-> Product Details-> Product Operation Window-> Settlement Window
This is a common logic line. There are five windows to look at. If you have time to try, you will find that when you open the settlement window, the whole program is panting and motionless.
Moreover, because MF is semi-real-time, GC cannot release resources immediately after closing windows. If users repeatedly open and close these windows, memory will overflow quickly.
So it's important to implement a window manager to make sure only one window is running at a time.
10. Reduce timer usage.
Timer is also a big consumer of performance. I have seen dozens of Timers open in a program, and the performance is simply terrible. So if possible, making sure that the entire program uses only one Timer, and starts it only when necessary, will take a lot of the burden off your program.
The above is how to talk about. NET Micro Framework performance optimization, have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.