473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

optimizing code: sort algorithm, random numbers etc. Best resources for optimization?

Hi, I have written code that I would like to optimize. I need to push it to
the limit interms of speed as the accuracy of results are proportional to
runtime.

First off, would anyone know any resources that explains how to optimize
code i.e. give some rules on c++ optimization? e.g. using memcpy to copy an
array (which i have done).

Also, what is the best sorting algorithm out there for sorting an array of
of size 100 or less? I have considered heapsort and insertsort. I am however
wanting to find the fastest.

Also I'm looking for a good fast random number generator. I am currently
using Boost:

boost::minstd_rand
boost::uniform_real<>
boost::uniform_int<>
boost::variate_generator<boost::minstd_rand, boost::uniform_real<> >
boost::variate_generator<boost::minstd_rand, boost::uniform_int<> >

Is the Mersenne-Twister RNG faster/better?

I am running VC++ 7 on an AMD Mobile XP 2600+ if that helps.

Thanks!
Cheers,
Peter
Jul 22 '05 #1
3 3299
PWalker wrote:
[bunch of questions not directly related to C++ language]

Most of your questions will be best addressed with a web search.
Say you start at www.google.com, and possibly with groups.google.com.
Socks

Jul 22 '05 #2
"PWalker" <p.******@rad.com> wrote in message
news:41********@dnews.tpgi.com.au...
Hi, I have written code that I would like to optimize. I need to push it
to the limit interms of speed as the accuracy of results are proportional
to runtime.

First off, would anyone know any resources that explains how to optimize
code i.e. give some rules on c++ optimization? e.g. using memcpy to copy
an array (which i have done). This may not always be a good idea. Beware of undefined behavior...
Also, what is the best sorting algorithm out there for sorting an array of
of size 100 or less? I have considered heapsort and insertsort. I am
however wanting to find the fastest.
Try std::sort. In popular C++ library implementations, it is based on
the IntroSort algorithm, which itself is an improvement over the famed
QuickSort. But the best approach will depend on the contents of the
array (e.g. sometimes a linked list based MergeSort, with guaranteed
NlgN complexity and reduced number of object copies may do better).
Also I'm looking for a good fast random number generator. I am currently
using Boost:

boost::minstd_rand
boost::uniform_real<>
boost::uniform_int<>
boost::variate_generator<boost::minstd_rand, boost::uniform_real<> >
boost::variate_generator<boost::minstd_rand, boost::uniform_int<> >

Is the Mersenne-Twister RNG faster/better?


How do you define "better" ?
To what extent is RNG speed relevant to your problem ?
The most critical rule when optimizing code is to *measure first*.
From your post, it does not sound like you have done much
profiling so far.
Do you know what percentage of the execution time is spent on
sorting or on other functions?
If not, it is likely that you are wasting your time.

Next, you need to look at algorithms complexity, and avoid
doing any computations when possible (e.g. caching, etc...).

Tertio, remember that memory access is the bottleneck of many
computer programs today. Try to make your memory accesses
contiguous and cache friendly (e.g. use in-lined data members
instead of pointers to separate objects; use arrays and std::vector
instead of other object collections when possible).
For C++ itself, all you need is to be aware of the cost of
some features (e.g. exceptions if relevant, virtual calls,
construction/destruction of objects, etc).

Also, remember to 'tune' the compiler options for generating fast
code. You may even consider switching compilers (some generate
noticeably faster code than others).
There are many targeted optimization techniques and approaches,
but you have given too little information about the kind of
problem that you are working on...
But again: measure first to know what part of your system
should be the target of your optimization efforts.
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #3
The answer to almost every "Which sort is fastest?" question is "It
depends."

The slightly smart bubble sort will kick butt on a list that was sorted
recently and has just had a few additions at the end.

When I needed these answers for work problems years ago, I could only
program each as best I could, then try them with normal distribution
(what I usually gave to the sort), then some completely random, then
some *almost* sorted samples.

Back then, I chose the second best (Shell) over the fastest (QuickSort)
because I actually understood Shell. (Could write the code as fast as I
could type.) but I could not remember Quick well enough to write or
troubleshoot in my head. And the time difference was minimal.

Yesterday I sorted 10,000 floats in 0.6 seconds with a not so great
algorithm. When I last did this that would have taken quite a long
time!

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
1
by: strotee | last post by:
#include <iostream> #include <ctime> using namespace std; // function declarations void swap(int *a, int *b); void sort(int arr, int beg, int end); int main() {
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
24
by: Richard G. Riley | last post by:
Without resorting to asm chunks I'm working on a few small routines which manipulate bitmasks. I'm looking for any guidance on writing C in a manner which tilts the compilers hand in, if possible,...
6
by: peter_k | last post by:
Hi, Last time i'm interested in optimizing small c programs. On my studies we are sending the programs using the web interface to online judge. The person who will wrote the faster program get...
3
by: Dennis Yurichev | last post by:
Hi. How can I find some ready tool or how can I find a proper way to develope tool which will convert such code: if (a>b+2+2) { f1(); f2(a*3*2); } else {
10
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.