Post: The way you write out your program matters
05-10-2013, 05:47 AM #1
Pichu
RIP PICHU.
(adsbygoogle = window.adsbygoogle || []).push({}); The way you write your code can be the difference between seconds, minutes, and hours for processing.

Below, I have an image showing the results of multiple tests, the tests showed on my computer roughly the exact same so I only select one result from each source as a median.

As you can see, just a little change in code has affected the speed of the application by several seconds. Not much but if used in the real world throughout the day, 7 seconds in an hour based upon the normal time this application takes, that's 140 seconds in an hour or 2 minutes and 20 seconds wasted. In a day, that's over half an hour.
You must login or register to view this content.
(adsbygoogle = window.adsbygoogle || []).push({});
05-15-2013, 11:25 AM #2
I'd like to note something in the second picture.

There you are declaring 2 new objects in a loop 1,000,000 times that is a horrible idea. (It's also the reason why it takes longer)
If it wasn't for the Garbage Collector that would be a horrid memory leak.

Avoid doing so at all costs, but if you must please add a dispose for the object in the loop when its no longer needed.
05-15-2013, 02:12 PM #3
Pichu
RIP PICHU.
Originally posted by Killer1478 View Post
I'd like to note something in the second picture.

There you are declaring 2 new objects in a loop 1,000,000 times that is a horrible idea. (It's also the reason why it takes longer)
If it wasn't for the Garbage Collector that would be a horrid memory leak.

Avoid doing so at all costs, but if you must please add a dispose for the object in the loop when its no longer needed.


Yep, but the main intention of this is just to show that the way you code matters. Both work in the fact that C# is a managed language but option 1 is the way to go because well, it's using object oriented programming more correctly.
05-15-2013, 10:40 PM #4
Master Ro
I make food
Originally posted by Killer1478 View Post
I'd like to note something in the second picture.

There you are declaring 2 new objects in a loop 1,000,000 times that is a horrible idea. (It's also the reason why it takes longer)
If it wasn't for the Garbage Collector that would be a horrid memory leak.

Avoid doing so at all costs, but if you must please add a dispose for the object in the loop when its no longer needed.



I think that was the point of the second picture, to show that you shouldn't instantiate variables in a loop.

Winky Winky
05-15-2013, 10:58 PM #5
Originally posted by Master
I think that was the point of the second picture, to show that you shouldn't instantiate variables in a loop.

Winky Winky


Well there is no real problem with in, considering running through 1,000,000 md5 objects and utf objects only added about 1:10 of extra time but I know the point is to show the extra time. Regardless, I just thought it would be a good thing to point out because it shows two pictures but there is no explanation of whats wrong.

Then again, when I started learning VB.net and C#, I had no idea of declaring something and using it within the same line so meh.
05-15-2013, 11:40 PM #6
Pichu
RIP PICHU.
Originally posted by Killer1478 View Post
Well there is no real problem with in, considering running through 1,000,000 md5 objects and utf objects only added about 1:10 of extra time but I know the point is to show the extra time. Regardless, I just thought it would be a good thing to point out because it shows two pictures but there is no explanation of whats wrong.

Then again, when I started learning VB.net and C#, I had no idea of declaring something and using it within the same line so meh.


The object of this wasn't to really to explain but to more of just show. It was to give users an insight as to the way that you code is difference between saving a few seconds.

Both work, if I wasn't using a loop I'd use the second image as it would clean up the code a lot more than the first.
05-15-2013, 11:51 PM #7
Originally posted by Pichu View Post
The object of this wasn't to really to explain but to more of just show. It was to give users an insight as to the way that you code is difference between saving a few seconds.

Both work, if I wasn't using a loop I'd use the second image as it would clean up the code a lot more than the first.


Yeah but no one gets to appreciate clean code but you, I'd rather use the primary method but that's just me because I find it simpler.
Ah well.
05-15-2013, 11:56 PM #8
Pichu
RIP PICHU.
Originally posted by Killer1478 View Post
Yeah but no one gets to appreciate clean code but you, I'd rather use the primary method but that's just me because I find it simpler.
Ah well.


Actual, I'm a supporter of open source programming. Most of the stuff I actually code is publicly released for people to view, use, and edit.
05-16-2013, 12:03 AM #9
Originally posted by Pichu View Post
Actual, I'm a supporter of open source programming. Most of the stuff I actually code is publicly released for people to view, use, and edit.


Yeah but neater code =/= easy to read. To anyone new who doesn't understand objects can be used in that method, might get confused. But its all theories.
05-16-2013, 12:55 AM #10
Master Ro
I make food
Originally posted by Killer1478 View Post
Well there is no real problem with in, considering running through 1,000,000 md5 objects and utf objects only added about 1:10 of extra time but I know the point is to show the extra time. Regardless, I just thought it would be a good thing to point out because it shows two pictures but there is no explanation of whats wrong.

Then again, when I started learning VB.net and C#, I had no idea of declaring something and using it within the same line so meh.



The fact is, you shouldn't instantiate new objects in code blocks. It just creates more work for the destructor. It's easier and more efficient to keep manipulating one object.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo