10 Replies - 5427 Views - Last Post: 12 October 2011 - 08:30 AM

#1 bluejacketpin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-September 11

How do I best speed up running of my prog?

Posted 05 September 2011 - 05:59 PM

How do I best speed up running of my prog?
Once I have simplified code as much as possible, made it smaller. By using loops were appropriate etc.

All ideas are welcome :D
Is This A Good Question/Topic? 0
  • +

Replies To: How do I best speed up running of my prog?

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: How do I best speed up running of my prog?

Posted 05 September 2011 - 06:53 PM

Give it multi threading support - I found that speeds up my apps a lot.
Hopefully its a solution!
Was This Post Helpful? 0
  • +
  • -

#3 juunas  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 87
  • Joined: 25-March 09

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 03:50 AM

Here's a link to an MSDN article on multithtreading:
http://msdn.microsof...v=vs.71%29.aspx
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 04:19 AM

Releasing the proper objects when possible. Every variable is more space taken up which slows speed, so proper variables, disposing of objects properly, etc is important.
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 05:05 AM

View PostjimmyBo, on 06 September 2011 - 02:53 AM, said:

Give it multi threading support - I found that speeds up my apps a lot.
Hopefully its a solution!

Just note that Multithreading doesn't speed up applications by default, you have to understand what you're doing and know exactly how and what to multithread before you start using them everywhere.

If used incorrectly, they have adverse effects on both speed and productivity. They can also add unneeded complexity to your code :dozingoff:

This post has been edited by RudiVisser: 06 September 2011 - 05:06 AM

Was This Post Helpful? 2
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 08:19 AM

Another option is to possibly change to a different algorithm.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,090
  • Joined: 02-July 08

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 12:39 PM

You can use the Stopwatch class to measure how long a chunk of code takes to process, and try diff ways to process that data. Also if CType data types several times for the same object it is faster to cast it into a variable and use the variable several times.
Was This Post Helpful? 0
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: How do I best speed up running of my prog?

Posted 06 September 2011 - 12:49 PM

As has been mentioned, you have two main options:

1) Optimization
2) Multithreading

Optimization should be the first thing you try if your code needs to be faster (it sound like you may have optimised already???), and then if you need further improvements after that, try multithreading.

For potentially significant increases in speed, you want to look into the subset of multithreading known as parallelism. That is the idea that you spread the workload over multiple cores.

Of course, for that to have any significant, positive effect, you need to have multiple cores on the PC that the application is running on.

If you do not have multiple cores, introducing any meaningful level of multithreading is likely to be harmful on performance, due to the extra performance penalties associated with multi threading (context switching being a key issue, for example, as may locking overheads). If you do have multiple cores available, you could increase the speed of the 'parallelized' operation by as much as a factor of the number of cores you have (if you have 2 cores, you can almost half the time it takes to complete the 'parallelised' operation).

To introduce parallelism into your application, you could manually create your threads (I would advise using the Task class if you want/need to take this approach) , handle the partitioning of work etc yourself. However, .NET 4.0 has some classes that are optimized for this parallelism specifically. These include following classes/technologies:

1) Parallel class
2) PLINQ
3) Task class

The PLINQ technology allows you to very very easily parallelise your LINQ queries.

The Parallel class primarily allows you to parallelise for loops and for each loops

The Task class allows you to fire up multiple threads (represented by tasks) that can 'parallelize' larger operations that are more specific to your application.


The Parallel class and PLINQ both partition/split the work into chunks, and execute each chunk on a different thread (often on a different core) automatically for you in a way that most efficiently balances the load across the cores.

For the Task class, you would have to handle the partitioning themselves (both the Parallel class and PLINQ use the Task class behind the scenes to spin up the required threads).


Just to give you an example of how powerful these technologies can be, here is a basic, unoptimized algorithm for calculating primes numbers from 5 to the passed in integer (using a for each loop, and it only calculates them and puts them in a list, just for this example):

 Private Function CalculatePrimeNumbersSequential(ByVal max As Integer) As IEnumerable(Of Integer)
        Dim primes As New List(Of Integer)
        For Each i In Enumerable.Range(2, max)
            Dim range As IEnumerable(Of Integer) = Enumerable.Range(2, Math.Sqrt(i))
            Dim all As Boolean = True
            For Each j In range
                If (Not ((i Mod j) > 0)) Then
                    all = False
                    Exit For
                End If
            Next
            If all Then
                primes.Add(i)
            End If
        Next
        Return primes
    End Function



I call that like this:

CalculatePrimeNumbersSequential(10000000)


so it is sequentially calculating the prime numbers from 5 to 10000000.

I am running a dual core laptop at the moment, and that sequential algorithm completes in an average of 28222 milliseconds. What's more, if I look at the CPU usage while that method is running, I see it hovering around 50%, showing that it is only using one core (as we are only using one thread!). Therefore, I have 50% spare, unutilised CPU, thus, I should, in theory, be able to half the time it takes to complete that method, if I make use of that extra 50% CPU (i.e. the other core on my laptop)....


Now, lets see if we can make it complete faster using parallelism (using the Parallel class):

Private Function CalculatePrimeNumbersParallel(ByVal max As Integer) As IEnumerable(Of Integer)
        Dim primes As IEnumerable(Of Integer) = New List(Of Integer)
        Parallel.ForEach(Enumerable.Range(2, max), New ParallelOptions() With {.MaxDegreeOfParallelism = Environment.ProcessorCount}, Function() New List(Of Integer), _
            Function(i, s, l)
                Dim all As Boolean = True
                For Each j In Enumerable.Range(2, Math.Sqrt(i))
                       If (Not ((i Mod j) > 0)) Then
                              all = False
                              Exit For
                       End If
                Next
                If all Then
                    l.Add(i)
                End If
                Return l
            End Function, Sub(l)
                              SyncLock primes
                                      primes = primes.Concat(l)
                              End SyncLock
                          End Sub)
           Return primes
End Function



That is the same algorithm, but it now runs, on average, 15,287 milliseconds. By spreading the work over BOTH cores (using two threads), we could do twice the amount of work executing per unit time, thus our execution time has halved! Plus, if I look at my CPU usage this time, it is consistently nudging 100%, indicating I am getting the absolute most out of my machine.

All the partitioning of work has been done for us. Usually, by default, the partitions of work start small, and get gradually larger and larger as the operation executes, as the workload increases. This is found to be the most generally efficient way of balancing workloads across cores.

Even with the introduction of locking (which I have, admittedly, minimised by using thread local storage) (each element enumerated is locked behind the scenes in the Parallel.ForEach() for thread safety). Imagine what you could achieve with a quad core machine, with a fully optimised algorithm!

That, right there, is the power of parallelism. You could, potentially, if you implement it correctly and sensibly, increase the speed of certain operations by simply huge amounts, just by utilising the extra cores available on your machine.

At the same time though, you could destroy the performance of the operation, but using the built in APIs at least helps to reduce the risk of that happening :).

Note that with this line part, New ParallelOptions() With {.MaxDegreeOfParallelism = Environment.ProcessorCount}, I am just making it absolutely clear and explicit that I don't want any more threads than I have processors, as that would start to harm performance (Parallel.ForEach() would very likely only start that many threads anyway).


Make sure you profile you code to identify key bottlenecks before mindlessly 'parallelzing' everything though, as multithreading (of which parallelism certainly is) does often add considerable (but unpleasantly subtle) complexities.


With PLINQ, it's even easier. It's as easy as calling a single method (AsParallel()) in fact, and your LINQ query is executed in parallel!


If you are not using .NET 4.0, you'll need to look into the ThreadPool and the Thread class. You will use them to get your threads up and running, and you will have to split the work across those threads manually. If you go that route, favour the ThreadPool class if possible :)

This post has been edited by CodingSup3rnatur@l-360: 06 September 2011 - 02:27 PM

Was This Post Helpful? 4
  • +
  • -

#9 Jakeyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 11

Re: How do I best speed up running of my prog?

Posted 01 October 2011 - 05:51 AM

I would reccommend multithreading.
Was This Post Helpful? 0
  • +
  • -

#10 jonkettle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-October 11

Re: How do I best speed up running of my prog?

Posted 12 October 2011 - 08:18 AM

  • As with life, always remember and value the 7 P's Proper Planning and Preparation Prevents Piss Poor Performance
  • Clean up your existing code to remove unnecessary logic.
  • Use one line code statements, expressions and methods as often as possible.
  • Get rid of reiterations where possible.
  • Use background workers or separate threads where applicable.
  • Use garbage collection to reduce active memory footprint of your application.
  • Keep large media content separate from solution (Do not compile it as a resource!!!)
  • When using media content such as sound, video, images and icons use a compressed version of the item if possible.
  • Use the right object for the job (dont declare as variable as an object when it is always going to be an integer, use 16bit integers for smaller numbers, etc)
  • Clean up your reference list (Not actually using something? Don't reference it)
  • In regards to your interface and program logic, as often as possible, KISS - Keep it simple, stupid!


For the most part being an organized individual can accomplish the above tasks, which will always result in better application performance.

This post has been edited by jonkettle: 12 October 2011 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#11 jonkettle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-October 11

Re: How do I best speed up running of my prog?

Posted 12 October 2011 - 08:30 AM

Oh also be sure to set the form DoubleBuffer property to True!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1