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 2 years ago.
Improve this question
I have two functions performing same process but with different techniques and I need to know on a large scale which technique is faster than the other of maybe in the future will be more techniques available. So my question is, how can I do that in c++ specially? Is there a specific method and header to be used to perform this task?
More details:
For example the isLargest() uses three parameters and it has two versions, one uses a nested if technique and the other uses initializers and less if statements. So if I need to know which one is faster, how can I do that?
Try your code in the real world and measure
There is a tool called a profiler that is meant to solve this problem. Broadly speaking, there are two kinds (note: some are a mix between the two):
Sampling profilers.
Instrumenting profilers.
It's worth learning about what each does and their pros/cons, but if you don't know what to use go with a sampling profiler.
There are many sampling profilers, but support depends on your platform. If you're on Windows, Visual Studio comes with a really nice sampling profiler and I recommend you start there!
If you go down this route, it's important to make sure you use your functions as you would "for real" when you're profiling them, as there are many subtle factors that can affect the result.
An alternative
If you don't want to try your code running in a real program, perhaps if you're just trying to understand general characteristics of the function, there are libraries to help you do this such as Google Benchmark.
Benchmarking code can be surprisingly difficult to get right, so I would strongly recommend using existing benchmarking tools where like Google Benchmark wherever possible.
Related
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 6 years ago.
Improve this question
I am trying to convert some code written in MATLAB to C++. I'm having some (or actually quite a lot of) trouble finding an alternative to the "fminunc" function which is used in the MATLAB code that I can replace and use in the C++ code. I've been looking at the "dlib"-library because I've heard it could be a function there I can use, but I'm not sure what function to use.
This is how the "fminunc" is used in the MATLAB code I want to convert:
[theta, cost] = ...
fminunc(#(t)(costFunction(t, X, y)), initial_theta, options);
Does anyone know any good optimizing functions like this in C++?
I believe what you are looking for is Google's Ceres Solver, an open source C++ library for modeling and solving large, complicated optimization problems. The code is designed to handle two classes of problems:
Non-linear Least Squares problems with bounds constraints.
General unconstrained optimization problems.
Automatic Differentiation is also supported.
Several cool example applications can be found here.
There are a bunch of optimizers in dlib, some that use gradients and others than just work on black-box functions. You can see some examples here http://dlib.net/optimization_ex.cpp.html and more generally here http://dlib.net/optimization.html.
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
After learning about c++ through a couple different sources, I found conflicting advice concerning the use of cout/printf(). One source said that printf(), and I quote:
... does not provide type safety, so it is easy to inadvertently tell it to display an integer as if it were a character and vice versa. printf() also does not support classes, and so it is not possible to teach it how to print your class data; you must feed each class member to printf() one by one.
So, the more important thing to me would be the readability factor of using printf(). On the other hand, another source mentioned that cout, with its use of the overloaded operator <<, uses more instructions to execute and can therefore be more expensive in terms of memory over a large program. Although, the person who said this was a systems programmer, in which every bit of performance is vital. But say I wanted to go into game or application development.
Would the performance differences between printf() and cout matter all that much?
In general, does it really matter what I choose to use in an application program?
Thank you for any input.
You would measure the differences on your particular implementation for your specific use case and determine that for yourself.
I would say both lines of reasoning in the question have merit, but you can't generalise about performance.
If you want to go into game or application programming, printf/cout won't be needed too much. The only use in these cases is for debugging.
If you really need to use printf/cout a lot, the difference will be when writing a huge amount of data, otherwise you don't need to bother.
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 8 years ago.
Improve this question
I am trying to develop a time table generator software for my college. Obviously it requires a great deal of constraint satisfaction i.e. I need to satisfy a lot of rules in order to generate a bunch of time tables where classes do not clash. After doing some research and reading this article, I feel I need to use some AI in it. Now, I am a complete newbie to AI. Can anyone tell me which algorithm will work best in my case?
The simplest algorithm that you can use for this problem is genetic algorithm (or any other evolutionary algorithm). Solving this problem using GA is very simple but yet effective. There are lots of papers and codes that have used this approach for this problem.
If you have few rules and constraints, you may want to use exact straightforward techniques like backtracking with CSP heuristics to speed it up, but if there are lots of classes and constraints, I suggest Genetic Algorithm.
Well, not a trivial task indeed. Problems like this one are VERY hard to solve.
Here I can recommend you two things:
Use an existing CSP/COP solver and describe your constraints in its language. These solvers are very good, fast and tuned, being developed for years.
Educate yourself in the area of Discrete Optimization (there was a course at coursera.org with the same name which was great). Only after you grasp the basics of how these things work can you try to write your own solver. But let you be warned! Discrete optimization is pain and suffering :-).
This is by no means a suitable place to just tell you how CSP/COP works. It is a very broad and difficult field.
I wish you good luck!
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 am supposed to build a program for storing and handling huge integers. I know that there are many answers out there but I need ideas that I can implement easily, bearing in mind that I can use any of the basic concepts of C/C++.
How should I go about it?
This is the first time I am asking a question here so please correct me if I am wrong about anything.
Edit: Actually what I wanted to know was how should I go about storing a huge integer... Obviously an array is what comes to mind at first glance but are there any other methods out there at the basic level?
EDIT2: I came across a very nice solution to this problem a while ago, but was just a bit lazy to put it on here. We can use the concept of number systems to deal with huge numbers. We can declare an array that holds the co-efficient of powers of 256, thus obtaining a base 256 system. We can then use fundamental concepts like those of the various number systems to obtain our required results.
Matt McCutchen has a Big Integer Library
If you want to do this yourself his code would be a great starting point. As you can overload arithmetic operators in C++ it is not too difficult to make a new BigInteger class and make this handle any number of bits per integer.
There is also a stack overflow answer to this question: here
I consider this as a question about theory, as such I suggest to browse the internet using the right keywords for documents/articles or to take a sneak peek at libraries that are implementing this feature and are well tested, this projects also tend to offer a mailing list or a forum where developers can communicate, it can be a good place to start writing about this stuff.
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 6 years ago.
Improve this question
I thought one of the best ways to familiarise myself with C/C++, is to make a helpful library. I was maybe thinking like a geometry library, like to calculate areas, surface area, etc. It would be useful in game programming. Or maybe an algebra library, like for different formulas like the distance formula, quadratic formula, etc. Or maybe like a standard library for very simple functions, like calculating the number of items in an array.
If it is for the sake of an exercise, writing a library to deal with fractions is a good one.
http://en.wikipedia.org/wiki/Fraction_(mathematics)
Implement the basic operations and a way to print them.
Find a problem that you need to solve. Look around to see if a library exists already. If not, then solve it in a way that others can benefit and put the library up on something like github.
But please be prepared to support it if you want to really see it being used - nothing worse than a open source project that isn't well supported.
I'd encourage you to try and come up with an application that would make use of the library. A game, a business app, whatever. Maybe even come up with an application idea first then determine what libraries you would need that aren't readily available.
That way you know you'll be creating something of practical value and not just undertaking a purely intellectual exercise. Try and avoid just plucking a library idea out of the air as you'll inevitably reimplement something that already exists. That's fine for you to learn but it would be awesome if you could create something others could benefit from in the process :)
Also, your application will provide a ready made test for your library.
Much of what you're listing has been done and can be found in Boost and or GSL. If learning is your goal, how about write a Qt application that uses some of these math functions?
Creating 'toy' level libraries doesn't help much on learning C++. I'd suggest you look at the libstdc++ bugs, try to understand and help fixing some ones.