Hi.
What about the suggested way to use subroutines and shared variables with them?
I mean, if I use subroutines with no shared variables, I think the code will be more clear and simpler to debug, but I´m afraid it is a not good for engine speed.
Thanks.
Subroutines, shared variables, etc.
Moderators: hgm, Rebel, chrisw
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
-
- Posts: 433
- Joined: Fri Dec 16, 2016 11:04 am
- Location: France
- Full name: Richard Delorme
Re: Subroutines, shared variables, etc.
I am not sure to understand what you mean.
Subroutine is a general concept that translates into function, method, etc. in modern languages and leads to the structured programming approach.
Shared variables is a feature of the programming language APL.
I guess you mean something else. Maybe you want to avoid the usage of global variable in your program?
Subroutine is a general concept that translates into function, method, etc. in modern languages and leads to the structured programming approach.
Shared variables is a feature of the programming language APL.
I guess you mean something else. Maybe you want to avoid the usage of global variable in your program?
Richard Delorme
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Subroutines, shared variables, etc.
Thanks abulmo2 for your time!abulmo2 wrote: ↑Sun Apr 11, 2021 6:07 pm I am not sure to understand what you mean.
Subroutine is a general concept that translates into function, method, etc. in modern languages and leads to the structured programming approach.
Shared variables is a feature of the programming language APL.
I guess you mean something else. Maybe you want to avoid the usage of global variable in your program?
May be I must said it before: I use FreeBasic. May be this is important to say.
Yes, I´m referred to global variables. If I do not use it, I understand every time you call a subroutine, there is a need to load again all the variables that have a value load in other part of the program. So I guess is a waste of time compared to use global variables. But on the other hand, not use global variables allow you to test your subroutines easier and make a less messed code.
Note: I´m not a programmer, so sorry if I asked a nonsense or very obvious things.
-
- Posts: 433
- Joined: Fri Dec 16, 2016 11:04 am
- Location: France
- Full name: Richard Delorme
Re: Subroutines, shared variables, etc.
Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.
For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
Richard Delorme
-
- Posts: 300
- Joined: Mon Apr 30, 2018 11:51 pm
Re: Subroutines, shared variables, etc.
In general: Don't micro-optimize until you have gotten down your large-scale structure first.
Making your program uglier by trying to send fewer variables to subroutines (functions) might save you 5% CPU time (just to say a number; take it with a grain of salt), and will easily cause bugs costing you days and weeks of development time. That time could probably be better spent implementing a better move generator, or a tree pruning technique, or tuning your evaluation.
Or moving to a faster language than FreeBASIC, which is likely to win you much more than 5%…
Making your program uglier by trying to send fewer variables to subroutines (functions) might save you 5% CPU time (just to say a number; take it with a grain of salt), and will easily cause bugs costing you days and weeks of development time. That time could probably be better spent implementing a better move generator, or a tree pruning technique, or tuning your evaluation.
Or moving to a faster language than FreeBASIC, which is likely to win you much more than 5%…
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina
Re: Subroutines, shared variables, etc.
So I understand you recommended to make each subroutine as "standalone code" like this:abulmo2 wrote: ↑Mon Apr 12, 2021 12:33 am Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.
For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
Sub test (ByRef a As Integer, ByRef b As Integer)
a=2*a
b=2*b
End Sub
Use ByVal is not as recommended?
Sorry but my english and my skills as programmer* are not good. What you mean with "For big custom type"?
*I did yet an engine that works without bugs, but its ELO is just around 1100 and its code is a big mess. So now I want to do something more tidy. i´ll go now for with a bitboards one but still not for multithread.
-
- Posts: 433
- Joined: Fri Dec 16, 2016 11:04 am
- Location: France
- Full name: Richard Delorme
Re: Subroutines, shared variables, etc.
For small data types, you can pass them by value, except if you want them to be modified externally.Luis Babboni wrote: ↑Mon Apr 12, 2021 1:09 amSo I understand you recommended to make each subroutine as "standalone code" like this:abulmo2 wrote: ↑Mon Apr 12, 2021 12:33 am Using global variables is usually not recommended. As they may be modified anywhere, it could then be hard to understand from where comes their values. However, chess programs are relatively simple and small, so tracking a global variable is not so hard in practice. If you plan to make your program multithreaded in the future, sharing the same variable between threads is probably not what you want.
For the cost of passing variables to subroutine, you can use a ByRef variable or a pointer to just pass the address of the variable instead of its value. For big custom type, it can make your program faster.
Sub test (ByRef a As Integer, ByRef b As Integer)
a=2*a
b=2*b
End Sub
Use ByVal is not as recommended?
Code: Select all
Sub test (ByRef a As Integer, ByRef b As Integer)
a=2*a
b=2*b
End Sub
x = 2
y = 3
test(x, y)
If you replace ByRef with ByVal, x and y should keep their original value of 2 and 3.
I meant this: https://documentation.help/FreeBASIC/KeyPgType.htmlWhat you mean with "For big custom type"?
If you have a lot of fields, it is faster to pass them ByRef than by ByVal.
Richard Delorme
-
- Posts: 4367
- Joined: Fri Mar 10, 2006 5:23 am
- Location: http://www.arasanchess.org
Re: Subroutines, shared variables, etc.
Generally speaking, it is harder to understand and debug code that might affect global variables in addition to whatever arguments are passed to the function (aka "side effects"). It shouldn't really affect speed though.
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Subroutines, shared variables, etc.
I would disagree only slightly. A program using only global variables and no subroutine parameters (or even worse, no subroutines at all ...) is probably 5% faster and 500% more buggy than a "normal" program.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
-
- Posts: 464
- Joined: Sat Feb 28, 2015 4:37 pm
- Location: Argentina