Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2006, 11:01 AM   #1
Pacer
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 Pacer is on a distinguished road
which is better?

Hello,all.Following is my homework,which is the better(faster and so on).
Thanks.
A.
#include <stdio.h>

int main()
{
        int i,j;
        for(i=0;i<26;i++)
        {
                for(j=0;j<26-i;j++)
                        printf(" ");
                for(j=0;j<i;j++)
                        printf("%c",j+65);
                for(j=i;j>=0;j--)
                        printf("%c",j+65);
                printf("\n");
        }
        return 0;
}

B.
#include <stdio.h>
int main(void)
{
        int i,j;
        int space;
        
        for(i=1;i<=6;i++)
        {
                int c=65;
                space=6-i;

                for(j=1;j<=2*i-1;j++)
                {
                        while(space>0)
                        {
                                printf(" ");
                                space--;
                        }

                        if(j==1)
                                printf("%c",c);
                        else if(j>1&&j<=i)
                        {
                                ++c;
                                printf("%c",c);
                        }
                        else
                        {
                                --c;
                                printf("%c",c);
                        }
                        
                }
                printf("\n");
        }
}
Pacer is offline   Reply With Quote
Old May 29th, 2006, 11:12 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Why don't you use your debugger or a pencil and paper and traipse around your code?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 1st, 2006, 4:29 AM   #3
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 58
Rep Power: 0 Adak is an unknown quantity at this point
The FIRST thing to remember in programming, is it's not an ultimate sprint. There will always be faster cpu's, etc., to move your program along. Speed HAS to take a back seat.

Your looking for:

*Accuracy,
*Clarity of the code,
*well-chosen algorithms,
*good interface for the end user's,

Then, speed!

If you're coding up something to intercept a speeding missile in mid-air, then speed gets moved up, considerably, but in general, the above characteristics of a good program, hold up well.

Your first choice code example is better for speed. If statements can cause a lot more work because of the way they're handled in the cache memory.

Adak
Adak is offline   Reply With Quote
Old Jun 1st, 2006, 6:06 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I think you're thinking of branching instructions at the machine code level. The cpu neither knows nor cares about "if" statements. Branching statements are generated at any point in the code that requires it, whether it's generated by an "if" or by some loop or switch construct. The OP should concern himself, rightly, with efficiently written code, and not run from "if" statements. Optimization is the province of those who know what they're doing in detail (and write good compilers).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 1st, 2006, 8:26 AM   #5
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
you can also use the "time" command in linux. type "man time" to find out more.
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jun 1st, 2006, 11:46 AM   #6
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
Ultimately, all these code, irrespective of being written in any language, will be compiled into machine code. So, the machine code that a CPU will understand and execute faster; will be faster. Am I right?

Possibly, the OP should understand, how each of his code, A and B, would take up CPU time, and then identify which of his code is better in terms of speed!
__________________
Visit: http://www.somaiya.edu
java_roshan is offline   Reply With Quote
Old Jun 1st, 2006, 12:33 PM   #7
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
One of my university courses teaches some stuff on timing, efficiency and whatnot. They also refer to the Big O or O(n) and stuff like that. I've still yet to learn it
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jun 1st, 2006, 2:14 PM   #8
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
@Serinth your referring to Big O Notation, I learned it it my first year java course at university. We laerned all about analysing algorithms, determining which ones ran more efficiently than others and why. O(n), O(logn), O(nlogn), O(n^2), O(n^3) is the notation for it. Not exactly fun stuff to pour through an algorithms and provide proofs as to why it runs O(n^2) or w/e, but it was interesting.
NSchnarr is offline   Reply With Quote
Old Jun 2nd, 2006, 12:56 AM   #9
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
NSchnarr, usually i just read how many loops or recursions it has to go through lol :\ I don't really go to class >:3
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jun 2nd, 2006, 11:04 AM   #10
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
Haha, yea that works quite well to just give you an idea of how the program runs. IE, two nested for loops both based off of n runs O(N^2), easy enough. My class required me to mathematically prove why, not so fun. End up with the same answer, but much more work.
NSchnarr is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC