Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: A question about varaibles that I should give to functions in C++

Author: Russell Reagan

Date: 01:39:57 08/04/04

Go up one level in this thread


On August 02, 2004 at 15:05:41, Uri Blass wrote:

>>Why do you think it would be slower?

>I know that programmers of programs like Greko had very slow code because they
>used C++ and not C.


The reason Greko is slow has nothing to do with C++. Greko uses the STL quite a
bit. That is not a bad thing. The STL allows you to do a lot of things easily,
quickly, and without creating a lot of bugs, but it is also slower. It is just a
tradeoff. The author of Greko preferred the benefits of the STL over having the
fastest program.

You can write C++ code that uses classes very heavily and is still equally as
fast as a program written in C. Just think of classes as structs that let you
have C-functions attached to them. Using a class will not be any slower than
using a function. In addition, you will get several benefits:

1. It will be easier to read your code
2. You will have fewer bugs (because of 1 and 3)
3. Bugs will be easier to find
4. You will be able to make changes much easier
5. You will be able to optimize better (because of 4)

This is what Dann Corbit said about how to write fast code.

1.  Write clear code.
2.  Choose good algorithms.
3.  Write abstract code that hides the implementatiion details when possible.
4.  When everything works well, profile it.
5.  Speed up the stuff that will benefit from it

Classes can help you accomplish numbers 1, 3, 4, and 5 in his list.

The bottom line is, don't worry about classes making your program run
significantly slower. In the end they will probably help you optimize your
program more effectively.


>>Gerd's point was actually that you can *gain* speed by using classes!  :-)
>>
>>Code that accesses global variables will have to carry the address of the
>>variable in the instructions. That makes the code larger, and potentially
>>slower.

>I cannot avoid accesing them and I use hply in many files of the search so I do
>not see an easy solution to get rid of it.
>
>The varaible hply appears in more than 200 places in my code.
>
>I prefer first only to convert time related varaibles to part of the class and
>not global varaibles that are used often.


This is a good example of why classes can be very helpful. You can "hide" things
in classes so that you don't have something scattered 200 places in your code.
If you wrap something up in a class, you only deal with it in one or two places
instead of 200 places, and then you can make changes much easier.

The idea is that each class is a black box. Classes don't need to know how other
classes work internally. Each class provides a way of manipulating it and
getting information from it. That part always stays basically the same, but the
internal parts are always hidden and you can change them as much as you want and
the rest of your program will continue to work.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.