Efficiency vs Memory tradeoff [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am creating an interactive sudoku board in c++. Whenever the user changes a value, I would like to check if the board is completed. The board will be completed when all spaces on the board are filled. My two ideas of how to do this are:
Create a private data member that holds the amount of filled spaces. To check if the board is completed I will simply have to check if this value equals boardLength^2
Create a member function that iterates through the board and returns false when a blank space is found and true if it goes through the board without finding any blank spaces
Is this a matter of preference, or is there a more accepted/correct way to do this?

Is this a matter of preference, or is there a more accepted/correct way to do this?
There is an accepted and correct way of optimizing, in general:
Optimize for speed or memory footprint when you actually need to, when you identify an actual problem. Your project's unique requirements will govern what constitutes a "problem".
Otherwise, optimize your code for readability and maintainability.
In your particular case:
Chances are that no matter which algorithm you choose, your check will happen so quickly that you will not be able to measure it, and the user will never notice the difference between the simple solution and the "fast" solution. Any attempts to optimize this (at the cost of complexity or readability or time spent writing code) are poor trade-offs.
Use the simplest possible solution. Once finished, if there is a noticeable delay on user input, and you can confirm that it's caused by an inefficient check for board completion, consider ways to improve your algorithm.

Related

How do I sort int without stl liberaries [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have a problem where I need to sort buses arriving at a bus station on the basis of time of arrival without using STL (standard template library) in ascending order
You may first want to read about sorting algorithms in general. A good staring point is here.
There you see many of them.
The recommendation for newbies is to start with bubble sort.
Please see here for an example including source code.
Then, you need to store your bus data in a struct. Along with the timing information. All those struct shoulb be stored in an array, best a std::vector.
Then you need to write a compare function for times. The complexity of this depends, if you have one varaible that stores the complete time, like in a unix timestamp, or in a struct, for example tm. Then you need to compare hours, minutes and seconds and some boolean relation.
But first, you need to read a lot, then think even longer on how to implement, and then write the code.

C++: Is it alright to keep creating new variables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to C++ and I have a general question. In order to solve any question in the exercises of the book I am learning from, while I am able to successfully solve the questions, I usually end up creating a lot of new variables within functions in addition to the ones that I have already initialised. For some reason, this worries me because I feel that I am writing inefficient code that might hog resources if I follow this practice for more complex programmes. Am I wrong in thinking this way? Are there any best practices regarding initialising and declaring new variables?
EDIT: I forgot to add, before resolving any question, I tend to convert the solution into plain English and then attempt to draw the program structure.
Normally compilers do liveness analysis of variables during the compilation of your code. Variables are considered live only starting from their assignment till their last use - optimizing compilers are capable of reducing the amount of local storage on the stack that is required by sequentially used variables (sometimes they even can eliminate their use entirely or keep them in registers only for a short period of time).

C++ sort() function algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Some days ago I wanted to use C++ sort() function to sort an array of strings, but I had a problem!
What algorithm does it use to sort the array? Is it a deterministic one or may it use different algorithms based on the type of the array?
Also, is there a clear time complexity analysis about it?
Does this function use the same algorithm for sorting numbers array and strings array?
It might or it might not. That is not specified by the standard.
And if we use it to sort an array of strings which the total size of them is less than 100,000 characters, would it work in less than 1 second(in the worst case)?
It might or it might not. It depends on the machine you're running the program on. Even if it will work in less than 1 second in worst case on a particular machine, it would be difficult to prove. But you can get a decent estimation by measuring. A measurement only applies to the machine it was performed, of course.

To factor code in function or not? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
A little question that I know many diverge on this but I would like to know performance and sanity wise, what do you use to do:
What's best, factoring code in function (when using the same piece of code in multiple places) but then having to face the function-call cost or just keeping those pieces everywhere then having to deal with changes in different places when you have to change the logic?
Considering the fact that I need my code to be the fastest possible. Because it will run on memory/cpu restricted device.
Maybe some of you have a rule of thumb they apply, like when the code is bigger than a certain amount of lign, they gather it in a function...
Rule of thumb:
Trust the compiler, in general it has better heuristics than you whether a code should be inlined. Write clean code. Code duplication is your enemy.
Measure the performance or check the generated code, and only try to optimize if you are unhappy with the results.
If there are problems, try to utilize templates to avoid code duplication and generate code at the template instantiation location.

Cheat-Proof techniques for Online Game(TCP) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a game and i'm really worried about hacking, so i'm writting a anti-cheat inside the game, i searched how the hacks now-a-days are written and i found some patterns, first of all, almost every hack uses a thread(Usually with CreateThread()) to create the checking loop(check if hack is on/off), do any of you have a good way to check if the thread is not from the game?PS:I also use threads from outside the code(inside DLLs)
It is almost certainly better to find a different way to "anticheat", since as cdhowie points out, there's nothing to prevent someone clever from splatting over your anticheat code with something that doesn't do what you want.
There are some techniques that work much better:
Let the server do all the "important" stuff, such as figuring out what score you get and how many lives you have left, and if the player becomes a zoombie (tries to move after he's dead), something is wrong.
Another method that I think works reasonably well is to basically record a "log" of how the player got to where they are - how many lives they used, how many enemies killed, what type of enemy, score of each enemy, weapon used, shots fired, how long it took to do all that, and then let a server verify that it's "reasonable" - so if someone ups the number of lives they have, or changes the weapon to create 100x the damage, or slows down the enemies or speeds up the players time, it will show up in the "log", and the log can then be discarded as "fake".