locked
Optimizing RRS feed

  • Question

  • The following excerpt piqued my interest . . . .

    INTERVIEWER: How do you decide on the trade-offs between speed and performance?

    GATES: Sometimes it’s a trade-off between adding features and executing really fast, but there are ways of having lots and lots of features and still making things fast. Basically you want to decide what the common cases are in a program, and make sure that they go straight through, that they don’t get bogged down with all these special case checks. If your main interactive loop has all sorts of checks in it, then your program is going to be slower than somebody else’s.

    Full article here: https://programmersatwork.wordpress.com/bill-gates-1986/

    My coding skills are getting better and I am starting to get the basics of programming. As I start writing larger and more complex programs, speed and performance will be a more relevant aspect I'll need to consider when I develop my programs. This leads to my question: What are some general tips to increase speed and improve performance?

    I searched the forum and found some posts that imparted some useful knowledge:

    Links to posts here:

    https://social.msdn.microsoft.com/Forums/en-US/8b55760c-6bed-40ea-bd59-db9c90b31bc3/optimization?forum=smallbasic

    https://social.msdn.microsoft.com/Forums/en-US/1bb8d377-3ae8-46c9-818a-24485bb66288/can-i-optimize-this-more?forum=smallbasic

    Info from posts (google as well) reiterated briefly here:

    1) Know when to use large variables (multi-dimensional arrays) vs. simple variables (x = 1). Simple variables are read faster but large variables can be useful to organize data and stored under one name. Look at my key stroke recorer as an example: THS130

    2) Use complex calculations when needed (use check point to prevent unnecessary calculations) in main loop

    3) Keep check points (if,else statements) limited in main loop

    4) Delete non usable data

    By the way, will smaller loops in a large loop (main loop) increase performance and speed?

    Are these points accurate and are there more that I should be aware of?

    I'll be updating these points and questions as I progress in my research.

    Thanks in advance.

    P.S. I give Jibba Jibba my full appreciation for providing the aforementioned article in an earlier post.




    • Edited by Ezra94 Wednesday, April 1, 2015 1:13 AM Add info
    Tuesday, March 10, 2015 5:00 AM

Answers

  • Hello Ezra94,

    Thank you for sharing information about optimizing.  This information will be useful for many programmers.

    Here are the other hints for optimizing:

    1. I think it's very important to know which part is slow in a program.  So I recommend to measure the speed in the program with Clock.ElapsedMilliseconds property.
    2. Many programs have nested loops such like as below.  To increase speed, inner loop is the most important in many cases.
      For j = 1 To 10
        ' Do something (outer loop)
        For i = 1 To 10
          ' Do something (inner loop - need to optimize first in many case)
        EndFor
      EndFor
    3. This is my case in game 2048.  To search next place for new "2" tile, I made an array space[].  This is for speed.  Because to find space from board[] will take a lot of time especially when remaining space are less.  To manipulate bigger array needs a lot of time.

    Nonki Takahashi


    Tuesday, March 10, 2015 9:16 AM
  • Hi Ezra

    Yeah that's top interview with a young Bill Gates. He got me thinking about simplifying. The absence of the unnecessary. A powerful approach to optimising I reckon.

    • watch out for build ups in your code, like storing stuff in arrays that just keep getting bigger.
    • nested loops should not do the same thing twice.
    • why not learn how to reverse a loop (avoid multi function calcs)
    • precalculations
    • use event handlers to handle events not a lot of other stuff (read litdevs article on advanced graphics)
    • writing code that predicts things can minimise bottlenecks (do stuff in the b/ground)
    • buffer stuff, e.g. File.ReadContents versus ReadLine
    • make d/loadable images the same size as they will appear in the program

    If you think something is going to load your loop up then time it to see how much.

    here's an old thread: https://social.msdn.microsoft.com/Forums/en-US/58417a0b-55af-4419-8f81-6f79cf28e051/programming-tips-some-general-programming-tips?forum=smallbasic

    Don't do things you don't have to do to get the same result.


    • Edited by Jibba j Wednesday, March 11, 2015 8:51 AM
    • Marked as answer by Ezra94 Wednesday, March 11, 2015 2:42 PM
    Wednesday, March 11, 2015 8:42 AM

All replies

  • Hello Ezra94,

    Thank you for sharing information about optimizing.  This information will be useful for many programmers.

    Here are the other hints for optimizing:

    1. I think it's very important to know which part is slow in a program.  So I recommend to measure the speed in the program with Clock.ElapsedMilliseconds property.
    2. Many programs have nested loops such like as below.  To increase speed, inner loop is the most important in many cases.
      For j = 1 To 10
        ' Do something (outer loop)
        For i = 1 To 10
          ' Do something (inner loop - need to optimize first in many case)
        EndFor
      EndFor
    3. This is my case in game 2048.  To search next place for new "2" tile, I made an array space[].  This is for speed.  Because to find space from board[] will take a lot of time especially when remaining space are less.  To manipulate bigger array needs a lot of time.

    Nonki Takahashi


    Tuesday, March 10, 2015 9:16 AM
  • Hi Ezra

    Yeah that's top interview with a young Bill Gates. He got me thinking about simplifying. The absence of the unnecessary. A powerful approach to optimising I reckon.

    • watch out for build ups in your code, like storing stuff in arrays that just keep getting bigger.
    • nested loops should not do the same thing twice.
    • why not learn how to reverse a loop (avoid multi function calcs)
    • precalculations
    • use event handlers to handle events not a lot of other stuff (read litdevs article on advanced graphics)
    • writing code that predicts things can minimise bottlenecks (do stuff in the b/ground)
    • buffer stuff, e.g. File.ReadContents versus ReadLine
    • make d/loadable images the same size as they will appear in the program

    If you think something is going to load your loop up then time it to see how much.

    here's an old thread: https://social.msdn.microsoft.com/Forums/en-US/58417a0b-55af-4419-8f81-6f79cf28e051/programming-tips-some-general-programming-tips?forum=smallbasic

    Don't do things you don't have to do to get the same result.


    • Edited by Jibba j Wednesday, March 11, 2015 8:51 AM
    • Marked as answer by Ezra94 Wednesday, March 11, 2015 2:42 PM
    Wednesday, March 11, 2015 8:42 AM
  • It's nice to see people thinking about this stuff.

    My favourite thing about SB is that it give us a chance to learn about these things in a simplified way. Nice!

    Wednesday, March 11, 2015 8:54 AM
  • Thank you Nonki for the tips and sample.

    Program Code: TLP645

    You provided a starter sample, in an earlier post, for a Tetris port I was writing (still working on it . . . .). While my project remains incomplete, I did learn several techniques and good practices from reading and dissecting it. This program displays many of the optimal techniques currently discussed such as,

    1) Large arrays we're mainly used to represent each major concept of the program: piece (any data relating to blocks) and dropped (data pertaining to dropping the blocks)

    2) Any data relating to piece is created before the main loop. It is retrieved as oppose to being created in the main loop.

    3) Calculations for moving a block we're done separately from initiating the actual movement of a block

    4) All variables are deeply tied in with each other. For instance, if I were to manipulate the data of gw and gh, the functionality of the program will remain intact. Another, the y axis increment of each block falling is 40 pixels, which is based on its own size and it turn makes up the game board itself. Using this logic allows one to predict the collision detection of each block.

    5) All these points support the fact that the logic is clear. This is especially helpful for readability sakes alone.

    Regardless of one's ability, there should be one technique to learn from this program. One may see more than I could see.

    A message to the reader, it's just a sample of collision detection. It's simply, blocks stacking on top of each other. An immense help nonetheless.

    Hope others reading this will gain the some insight.



    • Edited by Ezra94 Wednesday, April 1, 2015 1:14 AM Edit
    Wednesday, March 11, 2015 3:22 PM
  • Thank you Jibba Jibba for the advice and especially for providing the resources.

    I found two more post, which are related to your duck shooting game you made a couple years ago (I love that game!), that provide more information about optimizing a program.

    https://social.msdn.microsoft.com/Forums/en-US/f07a02e4-c66d-4d4a-b0ae-2f1fd574e4fd/duck-shoot-bug?forum=smallbasic

    https://social.msdn.microsoft.com/Forums/en-US/a7c56241-8d72-4dd9-b176-c467050219b5/duck-shoot-exe-only-runs-well-when-the-sb-ide-is-open?forum=smallbasic

    Could I put smaller loops in a main loop to optimize a program? I'm trying to find an example . . . .

    Wednesday, March 11, 2015 3:55 PM
  • I agree the beauty of small basic is its simplicity that allows beginners to learn the basics of programming with ease. Programming itself is still a challenge with small basic, however, it represents what programming is all about. Creating programs using basic concepts such as, loops and if statements and your own abilities logic and creativity to create applications (That's all it takes to get started). Yes, large languages require the same skill set, as previously mentioned, but they also require additional syntax, libraries, not to mention more code for each command, less tolerance with syntax errors and all this (even though they're very important) hinders a beginner from learning the meaning of each command (the basics!), which sadly daunts and frustrates many from approaching programming in the first place. Although, I can only say this from attempting Javascript as my initial language before moving to a more simple language, small basic.

    Please, don't get me wrong. I have immense respect for the larger languages. I have even greater respect to those who have managed to learn a large language from the start. In fact, it's part of the reason I'm learning small basic so I can learn them. A complex language has its own advantages and opportunities (much more), instead (as a result from learning small basic), I feel a renewed sense of excitement and confidence that I can approach a more complex language such as, javascript, java (which I plan on learning in late spring), or even C++.

    I don't think I would have given up If I hadn't found small basic but the learning process would have been a lot more difficult.

    Could you provide tips on approaching a large language?

    • Edited by Ezra94 Friday, April 17, 2015 2:54 AM Edit
    Wednesday, March 11, 2015 4:23 PM
  • Ezra94,

    Thank you for sharing this topic.  I'd like to conclude this thread as a TechNet Wiki article.

    But, the words speed and performance bother me.  I think those two are essentially the same.  Because speed is a time per something, and performance is a something per time.

    So trade-offs are between following couples for example:

    • Speed vs Accuracy
    • Performance vs Security
    • Performance vs Price

    Nonki Takahashi

    Monday, May 4, 2015 11:55 AM
  • Ezra94,

    Thank you for sharing this topic.  I'd like to conclude this thread as a TechNet Wiki article.

    But, the words speed and performance bother me.  I think those two are essentially the same.  Because speed is a time per something, and performance is a something per time.

    So trade-offs are between following couples for example:

    • Speed vs Accuracy
    • Performance vs Security
    • Performance vs Price

    Nonki Takahashi

    Ezra94 and Jibba Jabba,

    I wrote a draft of TechNet Wiki article to organize discussions in this thread.  Please review this article and let me know your comment if you can.

    http://social.technet.microsoft.com/wiki/contents/articles/30971.small-basic-optimization.aspx

    I think this article may help many SB programmers.  And the quality of the article is important for them.


    Nonki Takahashi

    Monday, May 4, 2015 2:59 PM
  • Hello, Nonki.

    Thank you for taking the initiative and writing a draft on this topic. I will look it over. Should I change the type of this post to "discussion" since we'll be using this post in the future? If I wanted to edit or add content to your draft now or in the future, as an on going project, do I do it from the article itself?

    Update: I looked over the article. This is a great start and you did a tremendous job adding the major ideas from the post into the article. It will surely make editing easier. I will fix some grammar errors, elaborate more on each topic, and add examples if that's okay with you. This will be a great learning experience for us. Again, thank you.

    I will continue to update the article with resources I find around the forum and branch out after I have exhausted all my resources here.
    • Edited by Ezra94 Monday, May 4, 2015 4:47 PM
    Monday, May 4, 2015 4:23 PM
  • Hi Ezra and Nonki

    Nonki nice article. You asked for thoughts on this stuff in "Other languages".

    Here's an article on approaching your next language: How Long should I use Small Basic before taking on a Big language?

    Optimising will always be on my mind because it's fun and makes programs run well.

    Other languages come with special features to help with optimising e.g. I think C# has a StreamReader thing happening.

    I still extreme program a bit, it sort of helps me dig deep, then I stop and think about what's wrong with it. Trial and error is part of coding. Testing.

    Your next language will be different in syntax and library but it will also work different and try achieve things in a different way - a different paradigm. That's heaps of fun.

    SB has given me a simplified opportunity to take a break from coding from time to time and seek to take a more top down approach. How can this program work better. Look for relevant articles. Continuous improvement. Try to see the program and not just loc.

    My favourite part in the young Bill Gates interview is the bit where he would dig code out of the garbage bin, go through it and try to simplify it.

    So what is Simple? The absence of the unnecessary? A new/better way of doing the same thing? I'm in no hurry to take a literal interpretation of simplicity.

    Maintain! Live to code - don't code to live. Programming is pure fun.


    • Edited by Jibba j Monday, May 4, 2015 10:37 PM
    Monday, May 4, 2015 10:26 PM
  • Oh, another nice thought I stumbled upon is Balance. A balanced approach to a program. There's also good reads on the pragmatic programmer.

    Monday, May 4, 2015 10:42 PM
  • Ezra94, thank you for edit directly to the Wiki article.  About usage of forum and Wiki, discussion in forum and concluded in Wiki is the best I think.

    This thread had questions so that Question was OK for this thread.  But actually we are discussing here.  So you can change this thread to Discussion.  I think both are allowed.

    By the way, I have a suggestion for you to apply TechNet Guru Contribution with the TechNet Wiki article.


    Nonki Takahashi

    Monday, May 4, 2015 10:55 PM
  • I agree the beauty of small basic is its simplicity that allows beginners to learn the basics of programming with ease. Programming itself is still a challenge with small basic, however, it represents what programming is all about. Creating programs using basic concepts such as, loops and if statements and your own abilities logic and creativity to create applications (That's all it takes to get started). Yes, large languages require the same skill set, as previously mentioned, but they also require additional syntax, libraries, not to mention more code for each command, less tolerance with syntax errors and all this (even though they're very important) hinders a beginner from learning the meaning of each command (the basics!), which sadly daunts and frustrates many from approaching programming in the first place. Although, I can only say this from attempting Javascript as my initial language before moving to a more simple language, small basic.

    Please, don't get me wrong. I have immense respect for the larger languages. I have even greater respect to those who have managed to learn a large language from the start. In fact, it's part of the reason I'm learning small basic so I can learn them. A complex language has its own advantages and opportunities (much more), instead (as a result from learning small basic), I feel a renewed sense of excitement and confidence that I can approach a more complex language such as, javascript, java (which I plan on learning in late spring), or even C++.

    I don't think I would have given up If I hadn't found small basic but the learning process would have been a lot more difficult.

    Could you provide tips on approaching a large language?

    Thank you Jibba Jabba to answer the question above.

    This is my opinion so far: many things are common for SB and other languages.


    Nonki Takahashi

    Monday, May 4, 2015 10:58 PM
  • I also remind the word Balance from the word Optimization.  There are a lot of trade-offs in a program.  So the Balance is important.

    Nonki Takahashi

    Monday, May 4, 2015 11:11 PM
  • Hi, Nonki.

    Here's another resource on Optimizing Loops



    • Edited by Ezra94 Wednesday, May 13, 2015 2:27 AM
    Tuesday, May 5, 2015 12:04 AM
  • Hello, Nonki.

    Can you elaborate more on Balancing?

    • Speed vs Accuracy
    • Performance vs Security
    • Performance vs Price

    What does security and price mean in programming? Can you provide any resources that introduce these topics? Also, I edited the wiki article. When you have a chance, please, review and let me know If I need to make any more improvements.

    Thank you.

    P.S. Sadly, I can't apply for techNet Guru Contribution because I'm not the original author of the article. Unless there is an exemption for editing articles, but I didn't find anything. Maybe you could apply. I think you deserve the credit for taking the initiative.

    • Edited by Ezra94 Wednesday, May 13, 2015 2:44 AM
    Wednesday, May 13, 2015 2:40 AM
  • Hi, Jibba Jabba.

    Can you expand a bit more on balance? Do you have additional comments on the factors Nonki brought up about balancing? Besides the ones previously mentioned, are there other factors that need to be balanced?

    If you have the chance, it would be much appreciated if you could review the wiki article: optimization and provide feedback.

    I will look up programmatic programmer and let you know what I find.

    Thank you.

    Wednesday, May 13, 2015 2:57 AM
  • Hi Ezra

    For me a "Pragmatic person/programmer" in this context is a practical person. One who will approach and do things that they know are most likely to work. They're likely to avoid doing something a certain way if it is likely to become too difficult.

    That said coding involves quite a bit of trial and error. I'll try this and see if it works. Might come with a new way of doing something.

    For me if I find I spend too much time stubbornly trying to make something work a certain way, then I try to balance up. i.e. pull back and take a simpler more practical approach.

    Sometimes I need to balance between what I would like a program to do and what my current skill is and also what the language is good for.

    Wednesday, May 13, 2015 9:54 AM
  • Ezra, I look forward to reading your thoughts on "what makes a pragmatic programmer".

    http://en.wikipedia.org/wiki/The_Pragmatic_Programmer

    citing the above (as at now):

    Characteristics of a pragmatic programmer:<sup class="reference" id="cite_ref-1">[1]</sup>

    • Early adopter / fast adapter
    • Inquisitive
    • Critical thinker
    • Realistic
    • Jack-of-all-trades

    Wednesday, May 13, 2015 10:07 AM
  • Other relevant topics worth web searching:

    • the balanced programmer (this returns sites on "the Healthy Programmer".

    Old basic coder gave us all great tip ages ago, something like:

    break a programming problem into parts, code that part, test it, get it working - when you get it working it's great for morale.

    • Edited by Jibba j Wednesday, May 13, 2015 10:25 AM
    Wednesday, May 13, 2015 10:21 AM
  • Hi, Jibba Jabba.

    I have been thinking about this topic zealously the last couple of years. I agree the pragmatic programmer is someone who possesses common sense but also a thirst for knowledge and the will to apply their knowledge to propel in life (to grow as an individual).

    Sometimes it's not what you know but how you approach the problem. By learning a craft pragmatically, the details will come naturally along with understanding patterns and gaining techniques. Make every day count and learn through the experience.

    One should still take the time to read and write but in conjunction to learning pragmatically. Like you've mentioned before, an aspiring programmer should spend 80% of their time practicing their craft and 20% reading about their craft. The key is to get a balance between interactive learning (doing) and static learning (reading).

    The following skills describes a pragmatic programmer or any pragmatic learner of any craft:

    • Communication Skills
    • Problem Solving Skills
    • Creativity Skills
    • Critical Thinking Skills
    • Open Mind
    • Planning Skills
    • Learning Skills

    Thanks for the link. There is plenty of reading material!

    In return, here is some reading material:

    So you want to be a video game programmer

    How to design programs

    Essential skills for an excellent career

    Ten ways to teach yourself to code

    What it takes to be a professional programmer

    Perhaps we can discuss each of these skills (bullet points) in context to programming and balance them without being so overwhelming.

    Another point that came to mind: make every day count and love learning. Crucial for self learners.

    P.S. I don't claim to have master all these skills, but I am pursuing a balance with these skills. Part of the reason, I mentioned these skills is so I could read other opinions and gain a different perspective. Knowing is one thing but applying and making progress is another. I'm constantly seeking self improvement and keeping an open mind.

    I'm going to twist your words bit If I may, "Live to learn, don't learn to live !"




    • Edited by Ezra94 Thursday, May 14, 2015 5:14 PM Add/fix links
    Thursday, May 14, 2015 2:42 AM
  • This amazing thread was featured in this blog post from Nonki: http://blogs.msdn.com/b/smallbasic/archive/2015/05/11/optimizing-small-basic-featured-thread.aspx

     

    Thanks to Ezra for getting it started!


    Ed Price, Azure & Power BI Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Thursday, May 14, 2015 2:42 AM
  • Thanks Ezra

    Just skimmed those 3 links you posted. Wow. Time well Ticked.

    Thursday, May 14, 2015 8:52 AM
  • Hello, Nonki.

    Can you elaborate more on Balancing?

    • Speed vs Accuracy
    • Performance vs Security
    • Performance vs Price

    What does security and price mean in programming? Can you provide any resources that introduce these topics? Also, I edited the wiki article. When you have a chance, please, review and let me know If I need to make any more improvements.

    Thank you.

    P.S. Sadly, I can't apply for techNet Guru Contribution because I'm not the original author of the article. Unless there is an exemption for editing articles, but I didn't find anything. Maybe you could apply. I think you deserve the credit for taking the initiative.

    These words are hit by web searching trade-off and speed or performance.  Security is to avoid attacks from malware etc.  Price is like a cost.  For commercial products, it will be their prices.  For an non-commercial product, it will be a number of developers and time to develop.

    About TechNet Guru: I will try to add you as the author of the article.  Actually I think the article is your idea. 


    Nonki Takahashi

    Thursday, May 21, 2015 7:48 AM
  • Ezra94,

    Congratulations!  Wiki article gets TechNet Guru Gold Medal.

    http://social.technet.microsoft.com/wiki/contents/articles/30971.small-basic-optimization.aspx


    Nonki Takahashi

    Saturday, June 20, 2015 4:40 AM
  • Nice article Ezra.
    Saturday, June 20, 2015 9:19 AM
  • Hello Ezra94,

    I wrote a sample code of profiler (to measure performance) for following thread:
    https://social.msdn.microsoft.com/Forums/en-US/29a41146-ed37-4079-b218-360b303e5e83/foul-sorcery-v11-performanceoptimization-help?forum=smallbasic


    Nonki Takahashi

    Sunday, June 21, 2015 2:33 PM
  • Hi Nonki.

    I wasn't able to run it. It could a problem at my end. Do you have a code profiler for another program? Perhaps one without assets? This sounds interesting and I wouldn't mind including a simpler version in the article about optimization.

    Correct me if I am wrong but it's basically a precise arrangement of text windows that displays the value of relevant variables in a program. That would mean it would vary by program?



    • Edited by Ezra94 Tuesday, June 23, 2015 1:42 AM
    Tuesday, June 23, 2015 1:38 AM
  • Security, performance and efficiency. Chuck in price and it gets real broad.

    Programmers are copying security flaws into your software, researchers warn

    One keystone article included in SB online resources discusses "re-useable code". I love re-usable code especially my own because I know how it works. I like others code when I know how it works. I don't like mysterious code or buggy code.

    I thought price was interesting. On the surface this is often discussed as coders under the pump to crank profits and free up resources for new problems. What about the social costs? e.g. If the dot GOV role out a new social program and it's up and running on time but buggy, it can really hurt those most vulnerable.

    Wednesday, June 24, 2015 3:30 PM
  • Hi Nonki.

    I wasn't able to run it. It could a problem at my end. Do you have a code profiler for another program? Perhaps one without assets? This sounds interesting and I wouldn't mind including a simpler version in the article about optimization.

    Correct me if I am wrong but it's basically a precise arrangement of text windows that displays the value of relevant variables in a program. That would mean it would vary by program?



    I wrote a sample: RZJ560.

    Nonki Takahashi

    Tuesday, June 30, 2015 5:03 AM