![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 4
Rep Power: 0
![]() |
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");
}
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobby Coder
Join Date: May 2006
Posts: 58
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
you can also use the "time" command in linux. type "man time" to find out more.
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
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
![]() |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
@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.
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
NSchnarr, usually i just read how many loops or recursions it has to go through lol :\ I don't really go to class >:3
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|