Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3 4

Author Topic: The Quest for Might and Knowledge: I have returned to programming  (Read 6230 times)

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #15 on: January 01, 2010, 03:32:56 pm »

"puzzleicious" is an invalid token; it's not a word.

In this context, it's a valid sentence with invalid tokens. A sentence with all valid tokens and invalid structure might be "Running John blue scissors." That sentence no verb, so is invalid.
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #16 on: January 01, 2010, 03:52:56 pm »

It is better to know that the compiler doesn't optimize unless you set it, than later optimize your code when you find it runs slowly but you never learnt how to enable optimization.

So here is my advice: One day soonish, learn the optimization options for your compiler, or at least the default level of optimization. Generally, you should look it up when/before you start having speed problems and decide to optimize the code. At this point, optimizing the algorithms is probably the correct answer, but don't bother replacing your n=n+1 when you might want to change the 1, or the n later, when the compiler can treat it as n++ transparently if you just enable optimization(Again, don't bother until you need it, and check the compiler optimization before trying to optimize the code itself. And finding a faster method of achieving the same result is usually the best choice, not finding faster code or having the compiler generate faster code.)
Logged
Eh?
Eh!

Rooster

  • Bay Watcher
  • For Chaos!!!
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #17 on: January 01, 2010, 04:15:31 pm »

Blacken: I know, that was explained in the book ;)

Qwertyuiopas: Thanks for the advice.
But do you mean, that in a context of a code rewrite, or there should be an option in a compiler?
Logged

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #18 on: January 01, 2010, 05:05:30 pm »

It is better to know that the compiler doesn't optimize unless you set it, than later optimize your code when you find it runs slowly but you never learnt how to enable optimization.

So here is my advice: One day soonish, learn the optimization options for your compiler, or at least the default level of optimization. Generally, you should look it up when/before you start having speed problems and decide to optimize the code. At this point, optimizing the algorithms is probably the correct answer, but don't bother replacing your n=n+1 when you might want to change the 1, or the n later, when the compiler can treat it as n++ transparently if you just enable optimization(Again, don't bother until you need it, and check the compiler optimization before trying to optimize the code itself. And finding a faster method of achieving the same result is usually the best choice, not finding faster code or having the compiler generate faster code.)
I'm sorry, but this is simply bad advice. I can't really sugar-coat it: it is bogus information that is not relevant to the topic at hand. Standard Python doesn't have an optimizing compiler*!

I know you mean well, but seriously, please. You're not helping. I would politely suggest that you should learn how Python works if you want to contribute, as that's the language he's currently using. And I'm taking my own advice here: I'm re-reading ThinkPython myself right now, in order to be better able to answer questions.


* - Other methods are used for code optimization that have little, if anything, to do with the tools running Python. The idea of an optimizing compiler in Python is so silly that it's been used as an April Fool's joke. I mean, for fuck's sake, man, Standard Python is an interpreted language to begin with, why are you even talking about compilers at the start? It's like bringing up a snowmobile in a car conversation, it's just got nothing to do with it.

(EDIT: Also, for people who are learning C++ and might be reading this thread: it's harmless-but-very-unnecessary advice there, to dive into optimization early on until you understand C++ absolutely god damned cold. The marginal performance benefit of hardcore compiler wank are almost undetectable below a staggering number of iterations--modern computers are simply Fast Enough and default compiler settings are smart enough that fiddling with them will not get you far. And if you're working on stuff at that scale where it starts to really matter, you should already know enough to go research the topic without qwertyuiopas telling you to do it. If you don't, you don't need it yet and are probably aiming too high.)
« Last Edit: January 01, 2010, 05:25:36 pm by Blacken »
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #19 on: January 01, 2010, 05:35:17 pm »

GCC, if you don't tell it otherwise, tries NOT to optimize very much. It tries to preserve the structure for debugging.


Note everything that follows is a more advanced topic, and not esssential to programming well, and it only applies to GCC. I went years without knowing it, just like I only recently learned that in C, static vars in the global scope are limited to use only from that file, kind of like all those OOP private things, just making it private to the source file rather than the class.
But knowing more allows you to do more in the long run.

And if your code is slow, and you are using GCC, before you go doing some crazy technique to speed up your code at the expense of readability(though if making it faster also makes it simpler or easier to comprehend, maybe it is a good idea...), consider increasing the optimization level first.

http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options
Quote
-O0
Reduce compilation time and make debugging produce the expected results. This is the default.
Quote
Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you would expect from the source code.

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.
Logged
Eh?
Eh!

Rooster

  • Bay Watcher
  • For Chaos!!!
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #20 on: January 01, 2010, 06:00:57 pm »

That's true what Blacken said. I'm putting C++ off for now.
Might become relevant in the future tho.
Logged

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #21 on: January 01, 2010, 08:03:44 pm »

qwerty. Are you reading what is being posted? This is not a C++ thread and you're talking about dreck that's absolutely meaningless to a neophyte besides--were this C++ he'd be at printf and you're telling him to look up how optimization passes work. Give it a rest. Seriously, dude. It's not helping anything. Like I said in my last post, if you're trying to be helpful, getting Python is where to be.
« Last Edit: January 01, 2010, 08:09:31 pm by Blacken »
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #22 on: January 01, 2010, 08:24:41 pm »

Optimization passes? Not at all! I am just saying, that if he eventually uses GCC, that he should at least find the general optimization options, specifically the -O#s, and recognize that the compiler doesn't automatically try to make the program run as fast as possible, and how to tell it when you want it to optimize heavily.

Understanding the specifics isn't important at all, at least not for a long time, and during that time he could benefit from just knowing the -O# ones.

And all of it is only important later, if he uses a compiled language.
Logged
Eh?
Eh!

Rooster

  • Bay Watcher
  • For Chaos!!!
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #23 on: January 01, 2010, 08:38:56 pm »

It would be great and all if I would actually understand a word of it
Logged

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #24 on: January 02, 2010, 02:52:24 am »

Optimization passes? Not at all! I am just saying, that if he eventually uses GCC, that he should at least find the general optimization options, specifically the -O#s, and recognize that the compiler doesn't automatically try to make the program run as fast as possible, and how to tell it when you want it to optimize heavily.
But he's not. Why are you still talking about it? Any user's manual for a compiler will surely have that information when he needs it.

EDIT: Rooster, he's talking about something particular to C and other compiled languages (languages that have to be run through a program and turned into an executable file before being run, more or less) that can optimize the code you write for better performance. It has precisely shit-all to do with Python, which is interpreted (the Python executable steps through your code line by line to analyze and understand it at runtime). This is a bad explanation of compiled and interpreted languages, but you can find more details in Chapter 1 of ThinkPython. Given how staggeringly un-useful it is to what you are doing, I'm not sure why he insists on continuing to bring it up, and I'm torn between thinking he's not actually reading the posts in this thread or he's desperate not to look "wrong" for some reason so he's bulling onward. The stuff he's talking about will be learned naturally if you ever go use a compiled language, and I promise you that you don't need to worry about it until then.
« Last Edit: January 02, 2010, 02:56:45 am by Blacken »
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235

Rooster

  • Bay Watcher
  • For Chaos!!!
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #25 on: January 02, 2010, 11:32:21 am »

Alright!
Onwards to chapters 3 & 4!
Logged

Rooster

  • Bay Watcher
  • For Chaos!!!
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #26 on: January 02, 2010, 01:00:22 pm »

http://wiki.sheep.art.pl/Z-Day

Hey, it's a roguelike in python!
How cool!

EDIT: To avoid a triple post I'm editing. Treat it as a new post.

Exercise 3.3 Python provides a built-in function called len that returns the length of a string, so the
value of len('allen') is 5.
Write a function named right_justify that takes a string named s as a parameter and prints the
string with enough leading spaces so that the last letter of the string is in column 70 of the display.

Huf, that took some trial and error, but I have something that works like I want it to, and I'm putting it here for evaluation. Simple programs shouldn't have bugs, but believe me first version was not so elegant. Identifing syntax errors is hard because python doesn't highlight places where the bug actually is .
Code: [Select]
def right_justify (s):
    space = 70 - len(s)
    print (' ' * space) + s
   
right_justify('allen')
« Last Edit: January 02, 2010, 01:57:49 pm by Rooster »
Logged

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #27 on: January 02, 2010, 05:03:18 pm »

Well, IDLE doesn't. Some other IDEs do. But I think you're better off sticking with IDLE for now at least, as it's very simple and, more importantly, forces you to see the errors yourself instead of having them pointed out to you. Later, for productivity's sake, you'll want an editor that does that, but for now it's probably better to learn how to do it yourself.

That function looks correct. You could make it more terse by doing something like

Code: [Select]
def right_justify (s):
    print (' ' * (70 - len(s)) ) + s

but that's a little harder to read and doesn't really save you anything. Most folks I know who use stuff like C, Perl, etc. advocate for terse code; I do not, I generally suggest you make every step as explicit as you can in order to make it easier for people to follow your code, and only do otherwise if you actually have to.
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235

Berserker

  • Bay Watcher
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #28 on: January 03, 2010, 04:51:33 am »

This works also, and it is easier to read.

Code: [Select]
def right_justify(s):
    print s.rjust(70)

But if you are learning, it is better to do it yourself rather than using functions that do everything for you.
Logged

Blacken

  • Bay Watcher
  • Orange Polar Bear
    • View Profile
Re: The Quest for Might and Knowledge: Rooster the brave and mighty wizards!
« Reply #29 on: January 04, 2010, 12:22:36 am »

This works also, and it is easier to read.

Code: [Select]
def right_justify(s):
    print s.rjust(70)

But if you are learning, it is better to do it yourself rather than using functions that do everything for you.
Shows how much Python text processing I do.  :P
Logged
"There's vermin fish, which fisherdwarves catch, and animal fish, which catch fisherdwarves." - Flame11235
Pages: 1 [2] 3 4