Recent

Author Topic: Percent, data type and performance  (Read 8609 times)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Percent, data type and performance
« on: May 23, 2010, 04:49:33 pm »
Hi,

I am programming a strategy game using Free Pascal/Lazarus.

I am going to store lots a data which are percentage values. What kind of data type is the most suitable :
  • Creating a type : TPercent : 0..100 and performing a calculation each time I need it : MyVar * (1 + Rate / 100) ?
  • Using a single (0.05 for 5%) which needs more memory .

Another question : What about debbuging and variable tracking under Lazarus ?

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Percent, data type and performance
« Reply #1 on: May 23, 2010, 05:15:23 pm »
What kind of data type is the most suitable :

In my opinion, the anwer depends mostly on the number of actual calculations you're going to make during the game.

In terms of memory, I think even lots of Singles won't be a problem in modern computers, so if these variables are used mainly for storing data, the Single type can be convenient. However, plenty of floating-point calculations may have a noticable cost in performance. In fact, you don't even have to declare Singles - it's enough you have the "/" operator to cause implicit floating-point division.

So, first of all, I suggest to look carefully and determine what calculations are really necessary, and do they have to be very accurate (otherwise you can approximate them with Integers).



« Last Edit: May 23, 2010, 05:17:24 pm by idog »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8099
  • Debugger - SynEdit - and more
    • wiki
Re: Percent, data type and performance
« Reply #2 on: May 23, 2010, 05:36:14 pm »
If you need the out most speed: integer.

But in 99% as idog said, unless you do millions of calculations, you will probably not notice.

Games usually have some form of input/output (text or graphic). Those operations usually take up far more time, than the calculations.

Anyway to answer the question:
use integers,
use DIV instead of "/"(keep it an integer division)

if you need a precision of  1% ( 0.01) => then write you app, so that all values are 100 times as big as they really are.

SomeVar := 100; // set the var to 1

And only when outputting the value, calculate the value the user should see.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10174
  • FPC developer.
Re: Percent, data type and performance
« Reply #3 on: May 26, 2010, 06:33:09 am »
Use 0..100, and only do the division when it is visible to the user, but not in own calculations

(iow >5%  is done by doing if <value> > 5)

 

TinyPortal © 2005-2018