Jump to content

What constitutes more efficient code?


Recommended Posts

I know this is not the ideal place to post this, but I cannot find it anywhere else. Stackoverflow literally blocked the question because of its content, and it further punished me by NOT LETTING ME SEARCH FOR IT. Worthless garbage website.

Now, when I was getting my programming degree, the professor did NOT talk efficiency with us. You've noticed all the complaints about people making highly inefficient spaghetti code? The reason for that is so that if an employer needs your code tweaked, they'll probably have to call you to do it rather than hiring someone else. Seriously. Because of this, our professor literally encouraged us to make spaghetti code, and even screw around making programs to come up with our own distinct style to further ensure our codes would be impossible for another to decipher.

Point is, I know nothing about what makes code efficient. I've heard you should minimize if-then statements, but I've found them hard to avoid. Even if I do find an alternative, I have no clue if its actually better or not.

Just for an example, let's say you're making a card game, where the cards can have all manner of effects. I've come up with two different ways to handle this. One is to use a chain of if-then statements (no else statements since cards can have more than one effect), that check to see which ability a played card has one by one and executes the appropriate lines of code. Another way would be to just have cards have variables indicating such things like how many cards you should draw or how much damage the card deals, among many other things. When a card is played, the program then goes through every possible affect executing them one by one with few if any if-then statements (I figured some things may still require an if-then statement; why have the player chose a target when the card has no ability that needs a target?). Here, literally every line will execute, while with the solution that involves if-then statements, every if-then statement will be tested by only one or two of them will have their conditions meant.

So, which would be better? I have no idea. I do not know the execution time of any command. I can often come up with half a dozen different ways of doing things but I have no clue which is better. What I need is a list that orders all the possible commands in every language I know based on efficiency. I thought about making this myself, but I can't find anyway to assign the system clock to a variable so I could make such a test program.

What constitutes 'better'? I know I can minimize the number of lines, but I've found that often makes highly convoluted code. Besides, is it really better? If you have 100 print statements, would it be more efficient or not to just have a for-loop iterate 100 times? That would reduce the lines, but would that really be more efficient? I have no clue. I know nothing about this. The internet doesn't seem to want you to know this either. Wtf?

If I could only pull the system clock, I could find this on my own. I could do something like
a=system clock

line of code

b=system clock

print(type of code, b-a)

That's all I want, but I cannot get that. I think I could do this in c++, but that obviously won't help me with pygame. Why am I not allowed to know this? Why? How can I possibly find it? Is it just forbidden to make efficient code? Why then do you get trolled all the time for it? What types of commands are more efficient than others? I have absolutely no idea.

Link to comment
Share on other sites

  • 4 weeks later...

The best way to efficient code is understanding fundamentally how the code you're running works. Understand the languages execution lifecycle, how garbage collection works (its implementations or lack thereof), when to use threading (and the drawbacks of parallel processing), variable scoping and how when you declare something how your application holds onto it memory (of some kind), producing effective data structures, what operations are computationally cheap vs expensive, etc.

If you're worrying about # of lines and standard output you're probably not at a stage where you actually need to worry about optimization past the basics. Your job in most cases would probably prefer something with a 1000ms execution time per request that they can maintain over a 100ms time built from gotos that every time a change is needing to be made the next programmer sends you an email hahah! Just worry about writing your code to be readable, maintainable and understand exactly what it is doing on the hardware you're running it with. When there aren't any mysteries left (at a high level) in the app you've written most of the code you write will be "fast enough" and further optimization can be achieved through experimentation and research.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...