Results 1 to 10 of 10

Thread: Help!! Code Optimizations :P

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8

    Help!! Code Optimizations :P

    Hi, I was wondering if there was anyone here who could look through a small header file of functions (4, small functions) and tell me how I could make them faster? Or, even better, do it for me, LoL

    I'm pretty new to C++, I've been studying VB from since I was 8 (I'm only 15 now :P), but I've decided to use C++ to get some speed and eventually fully step over into C++.
    I'm using VC++6 Standard Edition, and I know a little ASM, too, so any optimizations in ASM would be even more greatly appreciated.

    The file is attached. If you need my other source files, then I can send them, too. The main function, which the other 3 rely on, is RenderSingle, so any optimizations there are most needed.

    I'm going to be using the code pretty heavily, so I need to get it as fast as possible. Also, sorry if my post seems rude because I'm not too sure how to address people on a forum :P LoL

    Anyway, thanks to anyone in advance,
    Elche
    Attached Files Attached Files

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    I have looked at your code. I have some ideas of optimising it.

    Code:
    long GPUAPI RenderSingle(HDC hdc, BYTE *cart, int DrawX, int DrawY, int ZoomFactor)
    {
    	BYTE GV1; BYTE GV2; BYTE GV3; BYTE GV4; BYTE GV5;
    	BYTE BCnt = 0;
    
    #ifdef _DEBUG
    	long tc;
    	tc = GetTickCount();
    #endif
    
    	for (int y=0;y<8;y++)
    	{
    		GV1 = cart[BCnt];
    		GV2 = cart[++BCnt];
    		BCnt++;
    		for (int x=0;x<8;x++)
    		{
    			GV3 = (GV1 >> (7-x)) & 1;
    			GV4 = (GV2 >> (7-x)) & 1;
    			GV5 = (GV3 << 1) | GV4;
    			SetPixel(hdc, DrawX + x, DrawY + y, Color[GV5]);
    		}
    	}
    
    	if (ZoomFactor != 1)
    	{
    		StretchBlt(hdc, DrawX, DrawY, 8*ZoomFactor, 8*ZoomFactor, hdc, DrawX, DrawY, 8, 8, SRCCOPY);
    	}
    
    #ifdef _DEBUG
    	tc = GetTickCount() - tc;
    
    	return tc;
    #endif
    
    	return 0;
    }
    Unroll the for loop in red since it is only 8 loops. Looping involves branching which is slow. Your code will become longer but optimised for speed.

    That's all for today.

    I will tell you more tommorrow. It is night time here. I'm very sleepy now. I'm going to sleep now.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Replace all BYTEs by unsigned ints. The int is native to the CPU and calculations with it are therefore slightly faster.

    StretchBlt is slow, so the speed of your function doesn't really matter anyway, expect StretchBlt to take up ~90% of the total time the function needs.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW I have only looked at the function trans posted, don't know about the other functions yet, like trans I will now go to sleep.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    kk, thanks guys.

    @CornedBee: What do you suggest I use instead of StretchBlt?
    I have thought of using PatBlt to blt a square of ZoomFactor*ZoomFactor size to the hdc, how would that go?
    I have also tried not StretchBlt'ing but SetPixeling the larger sizes, that was far too slow...

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    After having second thoughts, I think you should not be bothered to optimise it since every function calls directly/indirectly RenderSingle() which uses setpixel().

    setpixel() is very slow; Recently, I wrote an app using it.

    All those basic operations of assignments and for loops takes place in terms of nanoseconds, if you optimise, you only save a few cycles, compare to setpixel() which takes in terms of microseconds.

    If you can, save those in bitmap files and load them once and bltbit or stretchBlt them. They are much faster.

    By the way, the file extension should be *.cpp, not h. Header files is for declarations and cpp is the implementations and definitions.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    You may want to consider DirectX Graphics.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    Hmmkay, thanks. Where can I get JUST the DirectX7 (NOT 8!!) headers and libs? I'm on a 56k modem so I don't want to download the full SDK - I have knowledge of DirectX pretty well so I don't need documentation, but I can't find anywhere that I can download JUST the headers and libs from... :P

    If possible could someone upload just them for me? It'd be helpful if they'd all be up for download, but if not, the Core, DirectDraw, DirectInput and DirectSound take preference...

    Thanks, again!

  9. #9
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    DirectDraw4 comes with your windows already But if you want 7, then you will have to get the whole deal or else it wont work... i mean, you have to get the entire sdk not just a few headers.... the sdk is about ~100MB probably less!
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You should somehow be able to get DX7 from MS. It might be hard, but it should be possible.

    The DX4 SDK comes with VS6, not windows.
    8 would come with VS.NET
    7 comes with quite a few game programming books.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width