Results 1 to 3 of 3

Thread: Speed of operations, Multiply versus Power?

  1. #1
    Hyperactive Member
    Join Date
    Oct 08
    Posts
    270

    Speed of operations, Multiply versus Power?

    Which is a faster way to square a number in vb6?

    Code:
    newval=oldval^2
    or
    Code:
    newval=oldval*oldval
    Knowing which is faster, and thus using proper optimization, could possibly speed up some of my graphics processing routines, which depend on processing MANY input values in as short a time as possible.

  2. #2
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,124

    Re: Speed of operations, Multiply versus Power?

    Code:
    newval = oldval * oldval
    Is significantly faster (empherical measurement - I generated 1,000,000 random numbers (Singles) between 1 and 99 and used both methods to square.
    Exponentiation gave 0.3125 Secs, Multiplication gave 0.0468 Secs)

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,548

    Re: Speed of operations, Multiply versus Power?

    When it comes to optimisation, the circumstances can be extremely important... and this is one of those cases where it is more likely than most.

    It could be significantly faster still to use either method for the calculation, but simply do it in advance and store the results to an array which you read from later. Of course that only works if you know that there is only a particular range of values that oldval can be (such as integers from -255 to +255).

Posting Permissions

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