Thread: How to Speed Up My Program

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    How to Speed Up My Program

    Hi, I am new to C++ and OOP, I entered a contest in which we must make a program that will play a board game against other players programs, and I want to speed up my program. File size does not matter, I just need raw speed.
    The contest will be run on a linux x64 computer, I am developing my program with Visual Studio.

    I read on the internet about ways to speed up my program, but I was wondering if anyone could give me some general C++ performance boosting tips.

    Basically, my program is built with a main "game" class, inside which are many "board" classes, which each contain about a hundred "square" classes. I read the structs are faster than classes, but making the program with structs was too complex.
    My program does lots of calculations with small integers (like between 0 and 10)
    I might add recursion into the program.

    I don't have time to start doing tricks with bits and stuff to make my program faster, but is there any general things I can do that will speed things up?
    By the way, the contest will be held on a 4 core computer, and I do have quite a few calculations to do that aren't connected to each other. Will threading my program really speed it up? If yes, then can anyone recommend me a threading tutorial that will be easy to understand, and give me tips on threading.

    Here is some general questions I was wondering:
    I read that ints are the fastest types, are they really faster then shorts or even chars for just holding small numbers and doing simple addition?

    How exactly am I supposed to pass class objects to other objects if it is for reading them only? Like this? void func(const MyClass& a)

    How much slower is it to read a int inside a class than it is to just read a int?

    A question not connected to speed: How do I make a class object inside a namespace, when inside the class itself, there are functions that use variables inside the namespace? Whichever I put first, the compiler complained that the other wasn't defined yet. Do I need to declare one first? I tried writing "class MyClass;" before the namespace, and after the namespace writing the whole class, but that didn't work. I don't want the classes themselves to be in the namespace. Please show me an example if you can.

    And when are namespace variables initialized? At the start of the program?

    Thanks for all the help, the contest will be over in about a week

  2. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Write/replace C++ specific things in C. Compile in C to fully optimize it (use the best compiling options you have like gcc -O3 etc). Identify bottlenecks with a profiling program and then tweak those bottlenecks with fancy things.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Get yourself a Linux box (as a separate machine, or dual-booting your current machine, or VmWare/VirtualBox on your current OS). Virtualisation won't matter, because what you're really after is the "before" and "after" differences.

    Then start using gprof on your program, with representative runs of your program.
    man page gprof section 1
    Profiling with GProf

    You can get some excellent profilers for VS, but they all cost $$$ x $$$
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The thing that matters most are the algorithms. No matter how much you micro-optimize a task that takes 1,000,000 calculations, you won't be able to outperform the same task solved by an algorithm that only requires 10,000 calculations.

    It's probably unlikely that you are able to optimize the program as a whole (unless there are common pessimizations all over the place). Instead you'll need to identify functions that take a large amount of time and that you could make faster.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Wow thanks for the fast replys, I'll check out Gprof.
    Will the Performance wizard in visual studio 2010 ultimate help me?

    @007: What exactly do you mean rewrite it in C? I can't make this program without classes, and I barely use C++ things anyways(like strings).(This is my first program in C++ and I never learned C++)
    I just do simple math on arrays of numbers.

  6. #6
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    It may not matter much then^

    If you can do calculations in parallel you may consider using multiple threads.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Will the Performance wizard in visual studio 2010 ultimate help me?
    Yes.
    Most people here only have the free express version.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by \007 View Post
    Write/replace C++ specific things in C. Compile in C to fully optimize it (use the best compiling options you have like gcc -O3 etc). Identify bottlenecks with a profiling program and then tweak those bottlenecks with fancy things.
    I'm not sure what you think would be gained by switching to C. Modern C++ compilers produce code that is just as fast as their C equivalents. I was able to prove this with GCC 4.2.1 on x64 linux when I was doing some bottleneck testing on a project at work. I wanted to see if I would get any more performance by switching critical regions of code to C, and saw less than 0.5% difference when full optimizations were turned on, and -march=native was used. in some tests, the C++ code actually performed faster.

  9. #9
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    ^like all performance tests they depend on the data being used and the exact situation. Not every situation will benefit from using C. And sure, C++ has some higher performance aspects as well. It totally depends on what it is being used for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM