Optimization Techniques


==============================================================================  
C-Scene Issue #05
Optimization Techniques - Part I
Platform: Non-specific
Steve Mertz
==============================================================================  

This is my first article on the subject of optimizations.  I plan to write 
several others as I come across new and nifty ideas on how to make your
programs run faster.  

This article is for the novice to intermediate programmer (most expert
programmers should know this stuff.) Most programmers wish that their programs
could run just a little bit faster (if not a lot faster.)  Well within this
and following articles I will give some simple hints for speeding up your 
programs.

This article will be dealing with two things: 1) Loops and 2) tables.

Loops!

  
  
One of the bottle necks of speed for most programs will be loops. Repeating a simple or complex item over and over. There are a couple of ways of helping this out. The first being loop unrolling and the second loop flipping. Loop Unrolling This is simply shortening the number of times you make a loop cycle. You do this by doubling or tripling the code inside the loop. Simple loop without unrolling. for (i = 24; i < 100; i++) moveShip(x, i); Same loop with unrolling. y = 24; for (i = 24; i < 100; i+=2) { moveShip(x, y++); moveShip(x, y++); } The second method is faster because it's checking whether or not we are done only every second time, rather than every time. This speeds things up because of the lack of over head of the testing. Loop Flipping Loop flipping is where you take your loop and build it upside down. This one is a little more technical why it works. When you are doing your for() loop there are two operations happening. One is a conditional jump and the second is an instruction jump. These can be costly if you have a lot of loop iterations to go through. Here is an example of how to fix this. Simple loop unrolled and no flipping. y = 24; for (i = 24; i < 100; i+=2) { moveShip(x, y++); moveShip(x, y++); } Same unrolled loop with flipping. i = 24; y = 24; do { moveShip(x, y++); moveShip(x, y++); i += 2; } while ( i <= 100) Again we used loop unrolling to help speed things up. Then by flipping the loops upside down (doing the condition at the bottom) we lose 1 conditional jump and 76 instruction jumps (one jump for each iteration.) So this speeds things up quite a bit. And it's simple to do. Tables
The next thing to think about is for those of you wanting to get into 3D engines or other heavy mathematical type programs where you deal with a lot of numbers that need to be calculated over and over is to use a table. Instead of using sine or cosine to figure out angles every time you need one you plunk them all into a table and use that. This does use up some memory but most computers these days have more than enough to spare for these tables. Here is a simple example. int i = 1; do { printf("%i ", i * 5); i++; } while ( i < 20); This simply prints all numbers divisible by 5. With that multiplication it slows things down. So to speed things up we would do. int i = 1; int countFive[] = { 5, 10, 15, 20, 25 ,30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 } do { printf("%i ", countFive[i++]); printf("%i ", countFive[i++]); } while (i < 20); Now that is a bit faster because we got rid of the complicated multiplication which would slow things down. but because we just use an array we use the index and look it up. And presto, we have the number. You can also use this method for sines and cosines. But instead of typing in all the tables, you could calculate all the values at start up. This will make smaller code, and also speed things up after it's ready to go. Stay tuned for more optimization tricks!

This page is Copyright © 1998 ByC Scene. All Rights Reserved
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值