PDA

View Full Version : All About Inc and IncEx - NaumanAkhlaQ



Naum
09-13-2008, 09:30 PM
All About Inc and IncEx

Hello and welcome to my short tutorial about Inc and IncEx. As you may or may not know there are alot of uses for this. Mainly this is used as an alterative to variable adding and subtracting.


Inc
Inc is basically Var := Var + 1, this may seem framiliar to you - rack your brains for this. Yes you guessed this is used in progress reports e.g Banked := Banked + 1;
So how do we use it?
Arguments for Inc are :

procedure Inc(var X);
Increases X with 1.
So its usage is basically Inc(Your Variable). For example instead of using Banked := Banked + 1; you can just do - Inc(Banked)
Simple, really? Well what if you want to add more than one to the variable?? Well read on...


IncEx
IncEx is basically the same as Inc as what it does is add or subtract from the variable. This is used when you want to add more than one onto a variable e.g

OresMined := OresMined + 27;
So How de we use it?
Arguments for IncEx are :

procedure IncEx(var X; N: Integer);
Increases X with N.
So its usage is basically IncEx(YourVariable, Howmuch you add to your var);. For example instead using OresMined := OresMined + 27; just do - IncEx(OresMined, 27)
Simple you see.

Other examples
Munk uses IncEx in his game to add to the score, but remeber you can also use inc like this : For i := 0 to 26 Do
Inc(Var);
Also you may be able to minus the values e.g IncEx(u, -34)

Hope you all learned something today, Peace out.

R0b0t1
09-13-2008, 10:34 PM
This is not really needed. I have never seen a thread asking about this.

Most compilers/interpreters directly change I := I + 1; to the INC opcode, so you mostly won't want to worry.

Harry
09-13-2008, 10:49 PM
EvilChicken! made this code R0B0t1. See how Inc(); > X := X + 1;

program FastTime;
var x,y: integer;
const Count = 1000000;
begin
ClearDebug;
WriteLn('Please wait a few seconds...');
X := GetSystemTime;
for Y:= 0 to Count do
Y := Y + 1;
ClearDebug;
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Y := Y + 1;');
X := GetSystemTime;
for Y:= 0 to Count do
Inc(Y);
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Inc(Y);');
WriteLn('');
WriteLn('Use Inc();!');
end.

I get

Took 1141 ms to do 1000000 incs using Y := Y + 1;
Took 984 ms to do 1000000 incs using Inc(Y);

Use Inc();!
Successfully executed
It is not much, but it still shows how much better inc() is.

Nice tut Naum :p

Waddo
09-13-2008, 11:03 PM
for Y:= 0 to Count do
Inc(Y);
that is bad codeing erm and incex(u,-34)

or you could do decEx(u,34) =]

dod did you drag out that tuorial

Timer
09-13-2008, 11:13 PM
EvilChicken! made this code R0B0t1. See how Inc(); > X := X + 1;

program FastTime;
var x,y: integer;
const Count = 1000000;
begin
ClearDebug;
WriteLn('Please wait a few seconds...');
X := GetSystemTime;
for Y:= 0 to Count do
Y := Y + 1;
ClearDebug;
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Y := Y + 1;');
X := GetSystemTime;
for Y:= 0 to Count do
Inc(Y);
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Inc(Y);');
WriteLn('');
WriteLn('Use Inc();!');
end.

I get

It is not much, but it still shows how much better inc() is.

Nice tut Naum :p
o_O
Took 782 ms to do 1000000 incs using Y := Y + 1;
Took 609 ms to do 1000000 incs using Inc(Y);

Use Inc();!

Wizzup?
09-13-2008, 11:18 PM
EvilChicken! made this code R0B0t1. See how Inc(); > X := X + 1;

program FastTime;
var x,y: integer;
const Count = 1000000;
begin
ClearDebug;
WriteLn('Please wait a few seconds...');
X := GetSystemTime;
for Y:= 0 to Count do
Y := Y + 1;
ClearDebug;
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Y := Y + 1;');
X := GetSystemTime;
for Y:= 0 to Count do
Inc(Y);
WriteLn('Took '+IntToStr(GetSystemTime - X)+' ms to do '+Inttostr(Count)+' incs using Inc(Y);');
WriteLn('');
WriteLn('Use Inc();!');
end.

I get

It is not much, but it still shows how much better inc() is.

Nice tut Naum :p

Meh. This is SCAR. In any other language Inc() would never be faster.

BazzBarrett
09-13-2008, 11:23 PM
quite a people still use
I := I + 1;
but
inc(I);
is faster easyer ad just basacly better nice tut (boy did u make it a hell of alot longer than i though i was exspecting 5 lines XD)

Naum
09-14-2008, 06:56 AM
Just made it. I find it more efficient, a procedure that does it for you ;)

But mind you, Inc probeblly looks like this:

Procedure Inc(Var X);
Begin
X := X + 1;
end;

:p

E: @ Waddo - If I find anything that people might need to know I make a tutorial about it.

Da 0wner
09-16-2008, 08:28 AM
You can also use this IncEx procedure :p.


procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
i := i + TheModifier;
if not Add then
i := i - TheModifier;
end;


i: The variable you want to modify.
TheModifier: The amount you want to modify it by.
Add: True/False True to add to i. False to subtract.

P1nky
09-16-2008, 10:48 AM
gj, i used it once or twice , but not really needed can be subsitute with numerous other stuff.

Janilabo
09-16-2008, 02:58 PM
Meh. This is SCAR. In any other language Inc() would never be faster.What does it matter though? We are using SCAR. So, if Inc(var) is faster in SCAR than var:= var + 1, then we definitely should stick with that.. I don't care about other languages, not at least while I am scripting with SCAR... ;)

Very nice little tutorial, Nauman!

Naum
09-16-2008, 03:18 PM
Thanks, for the post.
Also could inc be like this :p :

Procedure Inc(var x : Integer);
Begin
Inc(x);
end; :p

Simpler

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
i := i + TheModifier;
if not Add then
i := i - TheModifier;
end;


to

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
IncEx(i, TheModifier)
else
DecEx(i, TheModifier);
end;

Nava2
09-16-2008, 04:22 PM
By the way, IncEx is actually slower than doing Variable := Variable + Number;.

Just so you know.

Also, as others have mentioned, maybe add Dec(x) and DecEx(x, count) :)

Good tut though.

Wizzup?
09-16-2008, 05:13 PM
Thanks, for the post.
Also could inc be like this :p :

Procedure Inc(var x : Integer);
Begin
Inc(x);
end; :p

Simpler

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
i := i + TheModifier;
if not Add then
i := i - TheModifier;
end;


to

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
IncEx(i, TheModifier)
else
DecEx(i, TheModifier);
end;


If you remake Inc() in SCAR is will be slower than just doing + someNumber.
The only part that actually makes Inc() faster is because it is not compiled by SCAR. :rolleyes:

nielsie95
09-16-2008, 05:16 PM
Did you know that you can also Inc/Dec booleans?


Thanks, for the post.
Also could inc be like this :p :

Procedure Inc(var x : Integer);
Begin
Inc(x);
end; :p

Simpler

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
i := i + TheModifier;
if not Add then
i := i - TheModifier;
end;


to

procedure IncEx(var i : integer; var TheModifier : integer; Add : boolean);
begin
if Add then
IncEx(i, TheModifier)
else
DecEx(i, TheModifier);
end;


Both your Inc and IncEx would become a potential infinite loop =\

welcome
09-23-2008, 10:53 PM
[QUOTE=nielsie95;466441]Did you know that you can also Inc/Dec booleans?

I'm curious, how do you inc a boolean? And what is the use, if boolean is only true/false what would be the point? Unless it is like boolean[i]

bullzeye95
09-24-2008, 12:57 AM
[QUOTE=nielsie95;466441]Did you know that you can also Inc/Dec booleans?

I'm curious, how do you inc a boolean? And what is the use, if boolean is only true/false what would be the point? Unless it is like boolean[i]

A boolean is one bit. 0 or 1. So if it is true, it is really just the number 1. False would be 0. So all it would be doing is adding or subtracting from 0 or 1.

And Nauman, I'm pretty sure that inc looks something like this:
procedure Inc(var I);
asm
mov eax, [I]
inc eax
mov [I], eax
end;

EDIT: Seems I'm wrong. A boolean is a byte.. =/

nielsie95
09-29-2008, 08:24 PM
I think it's an enumeration:


program New;
type
THello = (Hi, You, There);
var
Hello: THello;
begin
Hello := Hi;
Inc(Hello);
case Hello of
Hi: WriteLn('Hi');
You: WriteLn('You');
There: WriteLn('There');
end;
end.

bullzeye95
09-29-2008, 09:11 PM
I don't think so, because when Hello goes past a value of 2, the case stops recognizing it, whereas if a boolean goes past the value of 1, it still recognizes that it is "true".

nielsie95
09-30-2008, 11:28 AM
BoolToStr recognizes as true, but take a look at this:


program New;
type
THello = (Hi, You, There);
var
Hello: THello;
b: Boolean;
begin
Hello := You;
Inc(Hello);
case Hello of
Hi: WriteLn('Hello = Hi');
You: WriteLn('Hello = You');
There: WriteLn('Hello = There');
end;
WriteLn('Hello = '+IntToStr(Variant(Hello)));
WriteLn('BoolToStr(Hello): '+BoolToStr(Variant(Hello)));
Inc(b);
Inc(b);
WriteLn('b = '+IntToStr(Variant(b)));
WriteLn('b = true: '+BoolToStr(b = true));
end.

bullzeye95
09-30-2008, 11:42 AM
Oh, somehow I tested it wrong. I had a case statement set up, but I did something wrong (I guess I forgot to put another inc :p).

Fine, you win, I lose.