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

Thread: [Any] A quick speed tip

Hybrid 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)

  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 48m
    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
    Aug 2013
    Posts
    6
    Userbars
    0
    Thanks
    0
    Thanked
    0/0
    DL/UL
    10/0
    Mentioned
    Never
    Time Online
    6h 25m
    Avg. Time Online
    N/A
    Quote Originally Posted by bamag View Post
    ...C++ looks so similar to Java :o
    well, they're both C-based languages

    ---------- Post added at 04:54 AM ---------- Previous post was at 04:47 AM ----------

    basic rule with loops/looping-mechanisms...

    use for if you know how many times you wish to repeat the test

    use do while if you wish to execute the test/loop AT LEAST ONCE, and then perform successive test(s) to see if condition still applies (prior to looping again)

    use while ONLY when you don't know how many times you wish to repeat the test, as while loops are more resource intensive.

    ---------- Post added at 05:04 AM ---------- Previous post was at 04:54 AM ----------

    Quote Originally Posted by Jehuty View Post
    ...the time it takes the compiler to declare and set an additional variable is barely measurable.
    with today's computers, you are correct UNLESS you're filtering through a large amount of data.

    yet "back in the day", when programmers only had 15-32k of RAM to work with, very strict methods/practices were adopted to manage that memory which would be viewed by today's programmer's as unnecessary; yet the trade-off is that programmer's today are not as resource conscious as they used to be and have a tendency of creating unnecessarily resource-intensive, or resource-hogging methods/functions/programs, simply because we haven't been as nearly constricted as our predecessors. But a good purist will always point these things out, or these factors are only worth looking in to when a program achieves a bottleneck... a good example would be when a C++ programmer is coding a game, only has a certain amount of memory that can be utilized at any given time, and may find it necessary to write in assembly (or even machine) in order to achieve optimal speed.

    Likewise, dudes in the olden days used to be more familiar with machine and assembly than today's programmers; as today's programmers are way super spoiled and are developing on computers with (at least) 4GB RAM, and (at least) 2 GhZ CPU

  10. #9

    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

  11. #10
    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

Posting Permissions

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