Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a particular request, I would like to know what could be the general rules to keep a code light and fast... I have written applications in VB.NET that generate EXEs up to 60 mb, and when they run, they are very slow and sometimes i can see the application like 'freezing' for some seconds even on my HP with 8Gb of RAM, expecially those who deal with speech recognition or contain a bunch of timers (from 2 to 5 usually). Particularly, in aplications where i use timers to scroll a content, i can see the scroll stopping sometimes for a second or two, and speech recognition sometimes fails like it is buffering.

So I wondered: are there any specific or general rules to optimize the code? Like: use this method instead of that.

Thanks for any help you may give.


EDIT:

I understand that the problem is very much related to the tasks my _Load event contains... as an example, the aplication I am developing in these month, is a speech recognition desktop environment than, when loaded, does the following:

1) starts 3 RadialBar Classes to show the time using 3 circular controls
2) reads an external configuration file and changes the desktop according to the string contained in the file (using StreamReader and a Do While loop)
3) creates a webclient programmatically to detect LAN ip, public IP, check the connection status, and show the result in 3 different labels
4) checks if the master user has been defined
5) defines a string that must scroll in 16 lines on the desktop (this is done by string concatenation)
6) starts the 3 timers: 1 for the scrolling text (set at 200ms) , 1 to retrieve the weather conditions via google (set at 15000 ms) , 1 do update date/time on the desktop (set at 1000 ms)
7) loads the command list from an external file using streamreader
8) starts the core program, the speech recognition engines, and populates the grammar (.LoadGrammar & .RecognizeAsync)

This is it.
In the rest of the project i have the timers definition that check the date & Time, connect to google to retrieve weather information, and the SpeechRecognized event that deals with the command given by voice.
I must admit that the UI is very heavy, it uses 1200x600 pizels images stretched to 1600x900 resolution as a desktop, a buch of listboxes and panels in the main window, and about 10 labels but i refuse to believe that a UI like this can put a 8Gb pc in crisis...

I tried to have a look at the Async programming page, but honestly i wouldn't know what to move to an async task.

Thank you

What I have tried:

I have tried optimizing loops, increasing the timer duration, but I could not see any reasonable improvement of performance
Posted
Updated 22-May-16 4:46am
v2
Comments
Mehdi Gholam 22-May-16 3:38am    
The general rule: measure everything, sort time taken in descending order and work on optimizing from there.

There is no "one thing" like "use this method instead of that" when it comes to optimisation.
It's a complex issue: the .NET compiler is pretty good at optimising, so it's the whole design of your app that needs looking at.
But that's not simple either: the first thing to do is find out the scale of the problem by timing the current application and establishing a repeatable reference point to measure improvements against. When you have that, start timing individual parts (use the Stopwatch class) to try and identify the areas that are taking the most time. You can then concentrate on those as they will give the biggest potential improvements.
There are a huge number of things to avoid: multiple timers is one. String concatenation is another - along with any other memory copying based usage such as adding and deleting to generic List objects without thinking carefully first. It could be that you are accessing SQL Server and running a stupidly long query, or returning huge amounts of data you don't need. We can't tell!
Trouble is, there are so many possible reasons for your code running slow that we can;t give you solid "do this" advice to fix it - you have to start working out where it's slow, then start looking at why. And we can't do that for you - we don't have access to your code, and probably couldn't run it if we did!
 
Share this answer
 
There are many rules to be followed while programming, VB.NET being a language of .NET framework, I would suggest the common tips that I regularly do suggest to others. Your executable, being 60MB is the first problem, why are you so eager at pushing 60MB of data when a simple executable of 1-5MB can load a basic application! Why don't you use modularization and create multiple DLL files to be used as libraries. This would literally decrease the size of your executable because most of the code would go in the libraries and won't be a hurdle while loading the application; linked libraries are linked later as needed.

This is the first step that you need to take, read this, How to create DLL in VB.NET - Stack Overflow[^]. Always create objects, libraries, files that are needed later. In the executable, just write the code that makes the application running. Later, start loading things one by one. Do not push everything in the starter loop.

That is not it. You also mentioned that your application "freezes", that is when you try to write everything and perform every action on the UI thread itself. .NET framework supports threading which can highly increase the performance of your application. Using Threads and Threading[^]. But that is not it, sometimes you have to literally push the code to background. In .NET framework, C# and VB.NET share this feature, this MSDN document would be helpful in guiding you with that, Asynchronous Programming with Async and Await (C# and Visual Basic)[^], asynchronous approach will solve maximum of your freezing problems; but doesn't guarantee 100% freezing free performance.

Finally, keep everything on a background thread. The UI thread must be left free for generating the graphics, for handling the user interaction, such as button clicks, input of keyboard etc. If UI thread is frozen, it doesn't mean application is stuck, most of the times it means that the application is processing a time-consuming task.

There are many ways to optimize the code, and there are many ways to make it bad. Just make sure, you're following the best practices provided by developers themselves. MSDN is famous for providing such guides and resources to developers. How about this? Coding Techniques and Programming Practices[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-May-16 8:51am    
Afzaal,

You really need to thing again. The advice of breaking the code into DLL won't help with performance and even with memory consumption at all. We all understand the value of modular design. First of all, you should talk not about "DLLs", but assemblies. If you break code into assemblies, you still have the same code fully loaded at the same time, no less. Moreover, it will happen even if you load assemblies dynamically via reflection. There is no a way to unload an assembly from an application domain. And only using separate application domains can help, because you can unload an application domain will all loaded assemblies. But then the developer would need to work through the boundaries between application domains, which is another hassle, actually, IPC, because domains are well isolate. This part of advice won't work.

As to the thread and async-related advice, the fair characteristics would be: it depends. There are many cases when adding some threads can only slow down processing.

The real problem is: we know next to nothing about inquirer's work. Indeed, the product is too heavy. We don't have any information to assume anything. One simple fact tells the tale: apparently, the inquirer does not understand that. If a person does not understand such things, I can assume about the quality of the code design, possible mistakes, and so on... well, anything. As you correctly say, there are too many ways to make it bad.

—SA
Afzaal Ahmad Zeeshan 23-May-16 2:50am    
Indeed, and it all boils down to just one thing that the OP needs to have a bit more understanding of and experience with language and framework. :-)
don't want to repeat Solutions 1 and 2.
Quote:
So I wondered: are there any specific or general rules to optimize the code? Like: use this method instead of that.
The only general advice is don't use naive approach when you need good performance.
After that, we talk about optimization, a profiler will give you hints about places to check.
Code optimization is a high skill, almost a specialty in programing. It involve some multi-facet code analyze, and this is a skill. I am usually get paid for this kind of service.
We can help you optimize a short routine in this forum, everything bigger will be out of scope of this forum.

[Update to Solution 4]
What matters is the way each task is done.
See Find numbers given sum and product using excel macro[^]
The OP made a very naive macro to solve the problem, it run in O(n^2) and only find a solution if made of integers.
A partial analyze leads to a macro that run in O(n) which only find a solution if made of integers.
A full analyze leads to a solution that takes O(1) and find all solutions.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900