![]() |
which is better?
Hello,all.Following is my homework,which is the better(faster and so on).
Thanks. A. :
#include <stdio.h>B. :
#include <stdio.h> |
Why don't you use your debugger or a pencil and paper and traipse around your code?
|
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 |
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).
|
you can also use the "time" command in linux. type "man time" to find out more.
|
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! |
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 :P
|
@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, usually i just read how many loops or recursions it has to go through lol :\ I don't really go to class >:3
|
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.
|
| All times are GMT -5. The time now is 7:54 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC