Results 1 to 10 of 13

Thread: [Any] A quick speed tip

Threaded View

  1. #1

    Joined
    Dec 2011
    Posts
    488
    Thanks
    303
    Thanked
    559/263
    DL/UL
    13/4
    Mentioned
    34 times
    Time Online
    1h 58m
    Avg. Time Online
    N/A

    [Any] A quick speed tip

    If you like making fast programs (I know I do!) one thing to consider (that is often overlooked) is looping.

    I'm not saying looping is slow, in cases it's needed, but take this code for example (C++):

    Code:
    vector<string> myvector;
    
    // ....
    
    for(int i = 0; i < myvector.size(); i++){
        // do stuff...
    }
    Notice where in the loop, you are calculating the size. In a looping structure such as this, the computer will be calculating the terminating condition (in this case i < myvector.size() ) every time the loop iterates. Though not major with smaller lists, in programs where you have a large list to process (that doesn't get modified as the loop is running), this can quickly add up.

    More calculations = less speed = more space needed to store the calculation = more heat generated inside the computer = CAN SUCK MAJORLY

    Easy way to speed up, save space, the environment, etc: create a variable to hold the size. In most languages depending on the structure a size() or count() operation can cost O(n), and running something that slow many times as you iterate through a list sucks.

    Code:
    vector<string> myvector;
    
    // ....
    
    int vsize = myvector.size();
    
    for(int i = 0; i < vsize; i++){
        // do stuff...
    }

  2. The Following 2 Users Say Thank You to Miguel For This Useful Post:

    j03 (06-19-2012),Trinket (11-11-2013)

Posting Permissions

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