Results 1 to 5 of 5

Thread: Streamline your code?

  1. #1

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Streamline your code?

    Hmm does anyone know what effect this would have at machine code level? The only reason at the moment that i continue to not streamline my code is for readability sort of.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Streamline your code?

    Define 'streamline'.

  3. #3

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Streamline your code?

    Here is an example...

    Code:
        If sInfo.nPos >= sInfo.nMax - sInfo.nPage Then
            DetectScrollLoc = True
        Else
            DetectScrollLoc = False
        End If
    streamlined...

    Code:
    DetectScrollLoc = (sInfo.nPos >= sInfo.nMax - sInfo.nPage)
    Edit:
    Computational tasks are often performed in several manners with varying efficiency. For example, consider the following C code snippet to sum all integers from 1 to N:

    int i, sum = 0;
    for (i = 1; i <= N; i++)
    sum += i;
    printf ("sum: %d\n", sum);
    This code can (assuming no overflow) be rewritten using a mathematical formula like:

    Code:
    int sum = (N * (N+1)) / 2;
    printf ("sum: %d\n", sum);
    The optimization, often done automatically, is therefore to pick a method that is more computationally efficient while retaining the same functionality. However, a significant improvement in performance can often be achieved by solving only the actual problem and removing extraneous functionality.

    Optimization is not always an obvious or intuitive process. In the example above, the ‘optimized’ version might actually be slower than the original software if N were sufficiently small and the computer were much faster at performing addition and looping operations than multiplications and divisions.
    Above quote is from wikipedia. I am just uncertain if it is a good method. Apparently some schools do not agree with doing such stuff.
    Last edited by Paul M; Sep 13th, 2007 at 07:45 AM.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Streamline your code?

    The first is a comparison, branch and store. The second is a comparison and store. The second will therefore be quicker as branches are a major slowdown.

    I don't know about VB but a good compiler might optimise that difference out.

  5. #5

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Streamline your code?

    Sorry *bump* may have just missed the edit above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width