8 Replies - 2604 Views - Last Post: 14 May 2012 - 03:26 AM

#1 Rhysorourky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-May 12

what makes code faster

Posted 05 May 2012 - 07:47 AM

i want to know what would make a program faster is it like say i made a very very simple fps and ist code was 1050 lines long and i made the same game but the code was 2050 lines long would the 1050 line game be faster or not what makes a program faster
Is This A Good Question/Topic? 0
  • +

Replies To: what makes code faster

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 610
  • Joined: 03-April 12

Re: what makes code faster

Posted 05 May 2012 - 08:54 AM

The number of lines doesn't correlate with speed very strongly.
It depends what the lines do.

Sometimes shorter code is more general that needs to calculate the situation, but longer code contains special cases - much faster. Sometimes long code contains needless actions - much slower.

Also your machine architecture has much to do with it. Especially the parallel capabilities.

Also, the simpler code you write, the easier it is to the compiler to figure out what you are trying to do - the better optimized code.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: what makes code faster

Posted 05 May 2012 - 09:05 AM

Better design.
Less wasted CPU cycles.
Less layering of calls.
Less use of higher level calls/frameworks.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1579
  • View blog
  • Posts: 3,007
  • Joined: 30-May 10

Re: what makes code faster

Posted 05 May 2012 - 09:27 AM

*
POPULAR

http://leto.net/docs...ptimization.php
What is absolutely critical is making sure you have implemented the best algorithms for the task. Get this wrong, and there's nothing any of the tools can do to fix it.

Before you even attempt to optimise anything, you need the following things
- a finished and bug-free (as much as any program is bug-free anyway)
- real world test data and/or use cases
- a profiler.

Trying to optimise with no information on an incomplete program is a blind man shooting in the dark. Use the profiler on the real code with the real data to find out where the real problems are.

Having found a hot-spot, you can then make a specific improvement.
Since you have a working program, you can tell whether your attempt to speed things up
- actually speed things up
- doesn't introduce any bugs

If it's buggy (or slower), then you can easily back out the change (you are using a revision control system aren't you).

Do not spray the "register" keyword all over the code. 40 years ago when C was first invented and the compiler had NO optimisation options, it was a good idea. Any modern compiler with any -O (or /O) options will invariably ignore it and make a much better job of register allocation anyway, without the need for programmer hints.

Likewise, most compilers are capable of performing substitutions like x <<= 1 in place of x *= 2 (and many more you can't think of). Keeping it nice and simple makes your job of writing bug-free code easier, and also makes it easier for the compiler to spot optimisations. If you try something obscure (like the pointless XOR variable swap), then the compiler will just give you what you asked for.
Was This Post Helpful? 6
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7304
  • View blog
  • Posts: 12,160
  • Joined: 19-March 11

Re: what makes code faster

Posted 05 May 2012 - 09:43 AM

What makes code faster is Moore's Law. That's maybe starting to slow down now, but decades of continuous improvement in performance have added up to one thing: it's much more important today to make your code maintainable than to try to alter it to save cycles.
Developer time is much more expensive than machine time in the vast majority of cases. Better to save fifteen minutes of deciphering convoluted (and marginally faster) code, once a year when someone has to make some sort of change, than to save fifteen CPU cycles in a calculation that the user never even realizes is happening.
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: what makes code faster

Posted 05 May 2012 - 10:05 AM

Quote

Less wasted CPU cycles.


that's really the only thing that matters; everything else just adds up to that. getting closer to the hardware is one of the best ways to reduce instruction cycles. reducing the number of instructions, replacing instructions with X cycles with instructions with fewer cycles(multiplication is typically a 3 cycle operation where as shifts and additions are generally 1 cycle instructions so replacing them where you can helps; division is even worse), and placing more restrictions on the language(like fortran can't have aliasing pointers) are also big ones.

there is also locality(reducing cache misses), and instructions scheduling. you want your program to operate in the same area of cache is much as possible to reduce instruction cycles for loading and unloading cache to different layers of memory. also, modern processors can execute multiple instructions at once if the data doesn't overlap so placing instructions that stay in the same cache but don't mess with each other can greatly improve performance.

the biggest thing you can do to improve performance is choose a better algorithm as the cycles you save can be exponential rather than additive or multiplicative. proper analysis of algorithms is important however and things like allocation take large numbers of cycles that can foil the best algorithms. the only way to really know what's best is to test.

This post has been edited by ishkabible: 05 May 2012 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,248
  • Joined: 28-March 11

Re: what makes code faster

Posted 05 May 2012 - 12:40 PM

Another thing which I think a Compiler should take care of for you (I would think) is to break register dependencies.
Was This Post Helpful? 0
  • +
  • -

#8 PapaStiffy  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 29-March 12

Re: what makes code faster

Posted 12 May 2012 - 12:01 AM

The only thing that matters for a computer programs speed are the processes needed to complete any given task. That being said, it is not necessarily the amount of lines (as this is just memory in the computer) but what those lines are doing (what processes are being completed and how many).

There are various methods to increase the speed of a program including the use of profilers, time tests and even assembly code optimizations... The best way to optimize your program is to design it correctly, use the right algorithms and data structures for the task at hand, and allow the program to do the least amount of work at any given time.

Even with all of that being said, don't worry about this until your embarking on a project that will require optimization... The compiler will usually do a lot of the optimizing for you, so until you are creating a substantially sized program or an incredibly demanding one, a few extra milliseconds are not often worth the extra work.

This post has been edited by PapaStiffy: 12 May 2012 - 12:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 179
  • Joined: 01-November 11

Re: what makes code faster

Posted 14 May 2012 - 03:26 AM

There's some good reading here on algorithm 'complexity' which refers to the resources that an algorithm consumes. For example, there are many ways to sort a list of things, but not all sorts are equally efficient, and furthermore, not all sorts are appropriate for all types of data.

http://en.wikipedia....s_of_algorithms
http://en.wikipedia..../Big_O_notation

Insertion Sort is an O(n^2) algorithm, which means that the time taken to complete the sort grows with the square of the input size (in the worst case).

Merge Sort, on the other hand, is an O(n log n) algorithm, so the time taken to complete the sort grows at the rate of the input size * log (input size) which is considerably faster.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1