1

I am making a program that makes sum of two binary number. All is ok and all working but i need some tips how to faster algorithm.

I am thinking about changing all variables to dynamically allocated variables. Is it good idea? Make all variables dynamic, even if i use it only one or only long strings?

I need advice on how to use dynamic allocated variables.

This is example one of my checking function:

bool Correct(string v1, string v2){
 for (unsigned int i = 0; i < v1.size(); ++i) {
            if ((cin.fail())||((v1[i] != '1')&&(v1[i] != '0')))
                return false;
                }
for (unsigned int i = 0; i < v2.size(); ++i) {
            if ((cin.fail())||((v2[i] != '1')&&(v2[i] != '0')))
                return false;
                }
    return true;
}

When i use this:

bool JsouVstupySpravne(string *v1, string *v2){...}

all usages like v1.size() stop working.

7
  • More related to your question, just saying "stop working" is not much to go on. How does it stop working? If you get build errors then you should include the complete and unedited build log in the question. If you get crashes then use a debugger to find out where in your code the crashes happens. If you get unexpected results then include the actual and expected results. Commented Nov 7, 2015 at 8:09
  • This code is not summing anything Commented Nov 7, 2015 at 8:11
  • 1
    Why the cin.fail() check? You don't read anything. And if you don't modify the strings, pass them as references to constants (e.g. const string& v1). Commented Nov 7, 2015 at 8:13
  • First of all thanks for tips. I am modify one of string (need same length). So i use: string *c1 = new string; In main function. Is it necessary to free memory before program end? wvdz - this is only one function to check Commented Nov 7, 2015 at 8:21
  • Your question is unclear: your are talking about summming things, but then your code just compares elements of the string. And, no, using dynamic allocation will not by any means speed up things. Commented Nov 7, 2015 at 8:24

3 Answers 3

2

You can give the references of input arguments for a start:

bool Correct(string v1, string v2){

can be

bool Correct(const string &v1, const string &v2){
Sign up to request clarification or add additional context in comments.

Comments

1

When using a pointer to a string instead of a string you need to change

string1.size()  →  string1->size()
string1[x] → (*string1)[x]

because the syntax C++ requires is different.

However C++ allows you also to use "references" that avoid making copies still keeping the string object syntax. To do that you can just change:

bool Correct(string v1, string v2){ ... }

to

bool Correct(const string& v1, const string& v2){ ... }

Note however that with today computers the execution speed depends on so many factors that "optimizing" the code without first having solid measures is just nonsense. You need first to find where the time is being spent (profiling) and only then invest in making the code faster (sometimes by making it uglier, unfortunately).

1 Comment

@DoctorCZE: No... you don't know. You simply don't know that you don't know. But you will know later :-)
1

If you are looking for an advice on performance optimization, you need to post the code that is responsible for most of the runtime. You didn't say which OS you are using, but there should be a profiler available which tells you that. On Linux it is gprof, VC++ includes one. Use this to find out where the time is spent, and than optimize the hotspots.

The best performance gains are usually achieved by using efficient algorithms, and then optimize the implementation. How do you convert the bit string to a number? The Horner scheme comes to my mind as an efficient algorithm which reduces multiplications.

Two points are worth mentioning in your code:

  1. The strings are copied everytime the function is called, but they are not modified. Use a const reference.
  2. Guessing from the fragment, you seem to use std::cin to convert the 0/1 char to a number. This is probably not very efficient, since iostreams do stuff you probably do not need in your case, e.g. locale handling.

Finally, there is a std::bitset type which implements what you want to do, if you can live with a fixed length of the binary string and a size of max. 64 bits.

unsigned long long Correct(const string &v1, const string &v2){
    auto const bits1 = std::bitset<64>( v1 );
    auto const bits2 = std::bitset<64>( v2 );

    return bits1.to_ullong() + bits2.to_ullong();
}

boost::dynamic_bitset should be an alternative if you need longer bit strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.