Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: [Any] A quick speed tip

  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)

  3. #2
    Reemer's Avatar
    Joined
    Dec 2011
    Posts
    639
    Userbars
    8
    Thanks
    364
    Thanked
    446/256
    DL/UL
    39/0
    Mentioned
    203 times
    Time Online
    4d 13h 47m
    Avg. Time Online
    1m
    Makes sense, but I never thought of this before! If you're only doing the myvector.size() for one operation, say if the loop only went once, would it be more efficient?

  4. #3

    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
    Quote Originally Posted by Reemer View Post
    Makes sense, but I never thought of this before! If you're only doing the myvector.size() for one operation, say if the loop only went once, would it be more efficient?
    Well if the loop is only going once, then I don't see why you're using a loop in the first place

  5. #4

    Joined
    Dec 2011
    Posts
    26
    Userbars
    1
    Thanks
    8
    Thanked
    11/8
    DL/UL
    4/0
    Mentioned
    17 times
    Time Online
    8h 45m
    Avg. Time Online
    N/A
    Quote Originally Posted by Reemer View Post
    Makes sense, but I never thought of this before! If you're only doing the myvector.size() for one operation, say if the loop only went once, would it be more efficient?
    It'd be roughly the same time considering it's doing the exact same number of calculations. I mean, the time it takes the compiler to declare and set an additional variable is barely measurable.

    Also, not a bad tip, but I'm fairly confident that some modern languages are actually smart enough to take care of this for you.

  6. #5
    bamag's Avatar
    Joined
    Dec 2011
    Posts
    656
    Userbars
    3
    Thanks
    40
    Thanked
    146/110
    DL/UL
    34/0
    Mentioned
    50 times
    Time Online
    N/A
    Avg. Time Online
    N/A
    I don't think it would be that much of an improvement. Both ways of doing the loop would be O(n) efficiency. Sure it'll be slightly faster, but it would probably be unnoticeable. Also this would not apply if the size() thing changed values. And jeez, C++ looks so similar to Java :o

  7. #6

    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
    Languages such as VB6 don't do this for you, which is annoying, especially dealing with larger arrays/lists and knowing that VB is already slow.


    Quote Originally Posted by bamag View Post
    I don't think it would be that much of an improvement. Both ways of doing the loop would be O(n) efficiency. Sure it'll be slightly faster, but it would probably be unnoticeable. Also this would not apply if the size() thing changed values. And jeez, C++ looks so similar to Java :o
    I mentioned that it wouldn't apply if the size didn't change values.

  8. #7
    bamag's Avatar
    Joined
    Dec 2011
    Posts
    656
    Userbars
    3
    Thanks
    40
    Thanked
    146/110
    DL/UL
    34/0
    Mentioned
    50 times
    Time Online
    N/A
    Avg. Time Online
    N/A
    Quote Originally Posted by ./m View Post
    Languages such as VB6 don't do this for you, which is annoying, especially dealing with larger arrays/lists and knowing that VB is already slow.




    I mentioned that it wouldn't apply if the size didn't change values.
    Hmm, I don't see it in your post but okay :p
    But I guess your tip is somewhat useful as it can reduce something that could take 2n steps to n+1.
    By the way, are you in college or high school? I thought you were older but your thread about physics made me consider otherwise :o

  9. #8

    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
    College most physics courses are taken sophomore year at my school and I'm already in 4th-year CS courses

  10. #9
    bamag's Avatar
    Joined
    Dec 2011
    Posts
    656
    Userbars
    3
    Thanks
    40
    Thanked
    146/110
    DL/UL
    34/0
    Mentioned
    50 times
    Time Online
    N/A
    Avg. Time Online
    N/A
    Cool, I'm still in high school lol. Taking Java course as a junior and going to take C as a senior
    CS is probably going to be my major :p

  11. #10

    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
    Yeah my HS didn't teach anything even related to computers, self-taught myself a lot

Posting Permissions

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