Raised This Month: $51 Target: $400
 12% 

Stupid question: "const" vs "#define"


Post New Thread Reply   
 
Thread Tools Display Modes
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-20-2016 , 06:27   Re: Stupid question: "const" vs "#define"
Reply With Quote #21

Quote:
Originally Posted by claudiuhks View Post
You guys really think that +/ - 4-512 byte(s) per binary file matter that much?
I think you just need to use const for constant stuff, stuff that should never change.

These look constant to me and are game-related. It's just an example.
The problem is that it doesn't even diminish the size, but controversaly increases it.

So then why not using define if they are faster and not increasing the size of binary as const.

Let me explain better with a table:

PHP Code:
TYPE                   Performance                File Output Size
---------------------------------------------------------------------------
define                   100% ( 1 ms)               100% ( 100kb 
const                    
70% ( 1.3 ms)               107% ( 107kb 

(it's just an example, i mean these values don't mean to be true)

So tell me why you choose const if it has worse characteristics?

Last edited by siriusmd99; 08-20-2016 at 06:29.
siriusmd99 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2016 , 06:57   Re: Stupid question: "const" vs "#define"
Reply With Quote #22

Output size doesn't matter. I'm not sure to understand what is the actual problem here.

What the hell are you trying to prove by showing random numbers without any basis?
I thought you would learn from you temp ban to stop to affirm things and confuse people if you have no idea what you're saying.

If you are going to prove something, please do it properly.
__________________

Last edited by Arkshine; 08-20-2016 at 07:00.
Arkshine is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-20-2016 , 07:57   Re: Stupid question: "const" vs "#define"
Reply With Quote #23

Why can't you stop to argue with me with no reason?

Did i write clearly here?

Quote:
Originally Posted by siriusmd99 View Post
(it's just an example, i mean these values don't mean to be true)
I just shown him example by a table and i had to put some values in it.
Ok next time I will put x and y instead, because you always find reasons to criticize me.

Last edited by siriusmd99; 08-20-2016 at 07:58.
siriusmd99 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2016 , 09:20   Re: Stupid question: "const" vs "#define"
Reply With Quote #24

Why can't you stop to affirm or repeat things like it's the very truth when you have no idea what you're saying?

Quote:
The aim of the "const" is to reduce binary file size
Quote:
For example i made a plugin with defines but it has too big size. So i can replace defines with constants to reduce file size.
No. You use const because the value should not change. It has no other meaning.

Quote:
Yeah it won't be so fast as defines
Quote:
if we think logically because defines are faster then const as defined are not called as constants and the time of execution is smaller
Did you look at the assembly code?
Did you try to measure the difference?

For decimal value, #define x 1 and const x = 1 give the same end result.
x will be replaced at compile-time for both. The only difference is with const compiler can check the value type. It can be useful if you work with float or tag value.

Using const value = 1 and new const value = 1 is not the same.
The former will be replaced at compile-time and the latter will be allocated on the heap/stack.
From assembly viewpoint, the former will load a literal value and the latter will load value from an address (both one instruction).
I'm not sure why you would use the latter. If a difference in speed there is, it should be definitively trivial.

Using #define string "hello" and new string[] = "hello" (const is irrelevant) with several occurrences of string in the code will give the same assembly output.
However, the former will allocate string several times while the latter just one time.


Quote:
So i better loose some miliseconds of execution but instead i will have a smaller size of the output file.
Because game/engine/amxx is monothread and because nowaday server has plenty of memory, speed should be favored. File size doesn't matter.
__________________

Last edited by Arkshine; 08-20-2016 at 11:09.
Arkshine is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 08-20-2016 , 11:14   Re: Stupid question: "const" vs "#define"
Reply With Quote #25

Quote:
Originally Posted by Arkshine View Post
Using #define string "hello" and new string[] = "hello" (const is irrelevant) with several occurrences of string in the code will give the same assembly output.
However, the former will allocate string several times while the latter just one time.

Because game/engine/amxx is monothread and because nowaday server has plenty of memory, speed should be favored. File size doesn't matter.
I'm still confused by this. If the assembly output is exactly the same, then there is no difference in performance. Which means the only difference should be size.

As constants I have:
29 strings
137 characters (including terminators)
+311 bytes filesize

As #defines I have the compiled equivalent of:
69 strings
278 characters (including terminators)

I'm not understanding how the theoretical wasted #define references could impact performance, unless this is wasting amxx runtime memory space, though you said yourself servers have plenty of memory these days. So unless you are hitting the high end and your plugin will not run anymore, I still don't see the negative effect this would have.

Obviously this is only a question out of curiosity, as there would only be an appreciable difference in a plugin with hundreds to thousands of string constants. I asked because the arguments presented in this topic seemed inconclusive, and still do, but try not to get at each others throats over it, it's not important.
__________________
Bad_Bud is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2016 , 12:16   Re: Stupid question: "const" vs "#define"
Reply With Quote #26

Yes, it should have no difference which makes sense.
As you have been asked, show us a simple test plugin which demonstrates there is an issue.
It will be faster and easier to understand what's happening.
__________________

Last edited by Arkshine; 08-20-2016 at 12:17.
Arkshine is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 08-20-2016 , 13:12   Re: Stupid question: "const" vs "#define"
Reply With Quote #27

DefineTest.amxx
PHP Code:
#define MYSTRING1 "MyString1"
#define MYSTRING2 "MyString2"
#define MYSTRING3 "MyString3"
#define MYSTRING4 "MyString4"
#define MYSTRING5 "MyString5"
#define MYSTRING6 "MyString6"
#define MYSTRING7 "MyString7"
#define MYSTRING8 "MyString8"
#define MYSTRING9 "MyString9"
#define MYSTRING10 "MyString10" 
ConstTest.amxx
PHP Code:
new const MYSTRING1[] = "MyString1"
new const MYSTRING2[] = "MyString2"
new const MYSTRING3[] = "MyString3"
new const MYSTRING4[] = "MyString4"
new const MYSTRING5[] = "MyString5"
new const MYSTRING6[] = "MyString6"
new const MYSTRING7[] = "MyString7"
new const MYSTRING8[] = "MyString8"
new const MYSTRING9[] = "MyString9"
new const MYSTRING10[] = "MyString10" 
PHP Code:
public plugin_init()
{
    
register_plugin("Test""1.0""")
    
    new 
Str[33]
    
    for(new 
i=0i<100000i++)
    {
        
copy(Str32MYSTRING1)
        
equal(StrMYSTRING1)
        
format(Str32"%s"MYSTRING1)
        
        
copy(Str32MYSTRING2)
        
equal(StrMYSTRING2)
        
format(Str32"%s"MYSTRING2)
        
        
copy(Str32MYSTRING3)
        
equal(StrMYSTRING3)
        
format(Str32"%s"MYSTRING3)
        
        
copy(Str32MYSTRING4)
        
equal(StrMYSTRING4)
        
format(Str32"%s"MYSTRING4)
        
        
copy(Str32MYSTRING5)
        
equal(StrMYSTRING5)
        
format(Str32"%s"MYSTRING5)
        
        
copy(Str32MYSTRING6)
        
equal(StrMYSTRING6)
        
format(Str32"%s"MYSTRING6)
        
        
copy(Str32MYSTRING7)
        
equal(StrMYSTRING7)
        
format(Str32"%s"MYSTRING7)
        
        
copy(Str32MYSTRING8)
        
equal(StrMYSTRING8)
        
format(Str32"%s"MYSTRING8)
        
        
copy(Str32MYSTRING9)
        
equal(StrMYSTRING9)
        
format(Str32"%s"MYSTRING9)
        
        
copy(Str32MYSTRING10)
        
equal(StrMYSTRING10)
        
format(Str32"%s"MYSTRING10)
    }

DefineTest.amxx
900 Bytes
T
otal time spent in string functions: 0.085721
Total time spent in plugin_init: 0.043563
Code:
date: Sat Aug 20 11:44:25 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000000 / 0.000000 / 0.000000
   n |                             copy |      10000 | 0.000243 / 0.000000 / 0.000014
   n |                            equal |      10000 | 0.000246 / 0.000000 / 0.000017
   n |                           format |      10000 | 0.000400 / 0.000000 / 0.000012
   p |                      plugin_init |          1 | 0.000450 / 0.000450 / 0.000450
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:44:55 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000001 / 0.000001 / 0.000001
   n |                             copy |      10000 | 0.000230 / 0.000000 / 0.000000
   n |                            equal |      10000 | 0.000223 / 0.000000 / 0.000000
   n |                           format |      10000 | 0.000380 / 0.000000 / 0.000000
   p |                      plugin_init |          1 | 0.000421 / 0.000421 / 0.000421
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:45:39 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000000 / 0.000000 / 0.000000
   n |                             copy |      10000 | 0.000227 / 0.000000 / 0.000000
   n |                            equal |      10000 | 0.000223 / 0.000000 / 0.000000
   n |                           format |      10000 | 0.000377 / 0.000000 / 0.000000
   p |                      plugin_init |          1 | 0.000423 / 0.000423 / 0.000423
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:46:21 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000001 / 0.000001 / 0.000001
   n |                             copy |    1000000 | 0.022548 / 0.000000 / 0.000001
   n |                            equal |    1000000 | 0.022521 / 0.000000 / 0.000005
   n |                           format |    1000000 | 0.038103 / 0.000000 / 0.000007
   p |                      plugin_init |          1 | 0.042269 / 0.042269 / 0.042269
0 natives, 0 public callbacks, 1 function calls were not executed.
ConstTest.amxx
910 Bytes
Total time spent in string functions: 0.085711
Total time spent in plugin_init: 0.043791
Code:
date: Sat Aug 20 11:44:25 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000004 / 0.000004 / 0.000004
   n |                             copy |      10000 | 0.000221 / 0.000000 / 0.000000
   n |                            equal |      10000 | 0.000227 / 0.000000 / 0.000001
   n |                           format |      10000 | 0.000387 / 0.000000 / 0.000001
   p |                      plugin_init |          1 | 0.000416 / 0.000416 / 0.000416
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:44:55 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000003 / 0.000003 / 0.000003
   n |                             copy |      10000 | 0.000244 / 0.000000 / 0.000001
   n |                            equal |      10000 | 0.000255 / 0.000000 / 0.000015
   n |                           format |      10000 | 0.000404 / 0.000000 / 0.000001
   p |                      plugin_init |          1 | 0.000512 / 0.000512 / 0.000512
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:45:39 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000004 / 0.000004 / 0.000004
   n |                             copy |      10000 | 0.000242 / 0.000000 / 0.000000
   n |                            equal |      10000 | 0.000221 / 0.000000 / 0.000000
   n |                           format |      10000 | 0.000378 / 0.000000 / 0.000002
   p |                      plugin_init |          1 | 0.000423 / 0.000423 / 0.000423
0 natives, 0 public callbacks, 1 function calls were not executed.

date: Sat Aug 20 11:46:21 2016
type |                             name |      calls | time / min / max
-------------------------------------------------------------------
   n |                  register_plugin |          1 | 0.000003 / 0.000003 / 0.000003
   n |                             copy |    1000000 | 0.022563 / 0.000000 / 0.000001
   n |                            equal |    1000000 | 0.022501 / 0.000000 / 0.000001
   n |                           format |    1000000 | 0.038068 / 0.000000 / 0.000004
   p |                      plugin_init |          1 | 0.042440 / 0.042440 / 0.042440
0 natives, 0 public callbacks, 1 function calls were not executed.
The only issue is that the original conclusions drawn by the topic seemed to be "only use #define if you think you'll only use the reference once, otherwise a constant is always better", and then sort of belittled and commanded people to go make good code by using only constants.

The real answer is it doesn't matter and everyone should write in a way that they find easiest to read and maintain. That should be the #1 goal for everyone, not splitting hairs over whether or not someone might judge you for preferring to use #define over constants.
__________________

Last edited by Bad_Bud; 08-20-2016 at 13:51.
Bad_Bud is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-20-2016 , 13:36   Re: Stupid question: "const" vs "#define"
Reply With Quote #28

Can't tell you right now why is there 10 bytes of difference (I don't have access to my PC and tools right now so I can't anylize assembly, several hundred kilometers away ), but try using a single define/constant several times instead of 10 of them once each.
klippy is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 08-20-2016 , 13:59   Re: Stupid question: "const" vs "#define"
Reply With Quote #29

I was going for a more practical scenario...

Replacing every reference with MYSTRING1 (so that MYSTRING1 appears 30 times within the loop) and then deleting the other 9 definitions at the top, the Constant version was significantly smaller: 730 bytes vs. 846. Which is really the expected result. In more practical settings, such as in my own plugin and the original test, constant seems to come out larger. In the case of my plugin, significantly larger.

It'd be interesting, if only for learning purposes, if someone could show how much allocated runtime memory is consumed by each version. I wouldn't know how to examine that, I'm just a curious noob.
__________________

Last edited by Bad_Bud; 08-20-2016 at 14:01.
Bad_Bud is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2016 , 14:20   Re: Stupid question: "const" vs "#define"
Reply With Quote #30

Where did you read 900 bytes exactly?

I tried to compile your code and I get :

Define:

Data size: 1392 bytes
Total requirements: 19604 bytes

New:

Data size: 584 bytes
Total requirements: 18796 bytes
__________________
Arkshine is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:26.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode