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

Thread: [Any] A quick speed tip

  1. #1
    Banned
    Join Date
    Dec 2011
    Gender
    Age
    25
    Posts
    490
    Thanks
    304
    Thanked 549 Times in 260 Posts


    Downloads
    13
    Uploads
    4
    Mentioned
    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    N/A
    Rep Power
    0

    [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++):



    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.


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

    Infamous Joe (06-19-2012),Trinket (11-11-2013)

  3. #2
    Reemer's Avatar
    Join Date
    Dec 2011
    Gender
    Location
    Iowa
    Age
    25
    Posts
    625
    Thanks
    348
    Thanked 414 Times in 242 Posts


    Downloads
    30
    Uploads
    9
    Mentioned
    200 Post(s)
    Time Online
    2 d 17 h 46 m
    Avg. Time Online
    2 m
    Rep Power
    8
    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
    Banned
    Join Date
    Dec 2011
    Gender
    Age
    25
    Posts
    490
    Thanks
    304
    Thanked 549 Times in 260 Posts


    Downloads
    13
    Uploads
    4
    Mentioned
    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    N/A
    Rep Power
    0
    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

    Join Date
    Dec 2011
    Posts
    26
    Thanks
    8
    Thanked 11 Times in 8 Posts


    Downloads
    4
    Uploads
    0
    Mentioned
    17 Post(s)
    Time Online
    8 h 32 m
    Avg. Time Online
    N/A
    Rep Power
    6
    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
    Join Date
    Dec 2011
    Gender
    Location
    127.0.0.1
    Posts
    659
    Thanks
    41
    Thanked 144 Times in 108 Posts


    Downloads
    34
    Uploads
    0
    Mentioned
    50 Post(s)
    Time Online
    N/A
    Avg. Time Online
    N/A
    Rep Power
    6
    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
    Banned
    Join Date
    Dec 2011
    Gender
    Age
    25
    Posts
    490
    Thanks
    304
    Thanked 549 Times in 260 Posts


    Downloads
    13
    Uploads
    4
    Mentioned
    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    N/A
    Rep Power
    0
    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
    Join Date
    Dec 2011
    Gender
    Location
    127.0.0.1
    Posts
    659
    Thanks
    41
    Thanked 144 Times in 108 Posts


    Downloads
    34
    Uploads
    0
    Mentioned
    50 Post(s)
    Time Online
    N/A
    Avg. Time Online
    N/A
    Rep Power
    6
    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
    Banned
    Join Date
    Dec 2011
    Gender
    Age
    25
    Posts
    490
    Thanks
    304
    Thanked 549 Times in 260 Posts


    Downloads
    13
    Uploads
    4
    Mentioned
    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    N/A
    Rep Power
    0
    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
    Join Date
    Dec 2011
    Gender
    Location
    127.0.0.1
    Posts
    659
    Thanks
    41
    Thanked 144 Times in 108 Posts


    Downloads
    34
    Uploads
    0
    Mentioned
    50 Post(s)
    Time Online
    N/A
    Avg. Time Online
    N/A
    Rep Power
    6
    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
    Banned
    Join Date
    Dec 2011
    Gender
    Age
    25
    Posts
    490
    Thanks
    304
    Thanked 549 Times in 260 Posts


    Downloads
    13
    Uploads
    4
    Mentioned
    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    N/A
    Rep Power
    0
    Yeah my HS didn't teach anything even related to computers, self-taught myself a lot

Page 1 of 2 12 LastLast

Posting Permissions

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