Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 572 573 [574] 575 576 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 818152 times)

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8595 on: December 05, 2015, 06:19:09 pm »

Well, it doesn't make any sense to store the data in two places at once. I suppose I could use pointers instead of the indexes I have now, but I don't like doing that in Python because half the time it just creates a new object instead of behaving properly.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8596 on: December 05, 2015, 06:23:45 pm »

Learn how to use them properly, then. Keep your 2d array of references to tiles, each of which stores a reference to its province, and then just have every province store a list of tiles.
It'll probably be much faster and cleaner in the end, even if you're using more memory.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8597 on: December 05, 2015, 06:25:59 pm »

I should clarify. That's basically what I'm doing now, it just makes the code hard to read. Unless I use memory addresses, which Python isn't really designed for.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8598 on: December 05, 2015, 06:28:45 pm »

world.provinces[world.tiles[(x, y)].province].adjacent is used twice there. Store it in a descriptive variable beforehand to increase readability and probably also speed.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8599 on: December 05, 2015, 06:37:22 pm »

Oh, right, that's what you meant. Derp.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8600 on: December 05, 2015, 06:40:59 pm »

No, not really, I just didn't initially in the effort to try and read the code snippet and instead looked at your explanation :v
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8601 on: December 05, 2015, 07:30:24 pm »

Sometimes I wonder if I even have the foggiest idea of what I'm doing.
Code: [Select]
if world.tiles[i].province not in world.provinces[world.tiles[(x, y)].province].adjacent:
    world.provinces[world.tiles[(x, y)].province].adjacent.append(world.tiles[i].province)

This particular example would be improved by letting .adjacent be a set instead of a list, but whether that's a good idea depends on the rest of the code.

It also looks like you're inside a loop that looks like
Code: [Select]
for i in len(world.tiles):
    process(world.tiles[i])
That would probably be faster and cleaner as
Code: [Select]
for tile in world.tiles:
    process(tile)
(Please tell me you're not using a while loop and incrementing manually.)

Well, it doesn't make any sense to store the data in two places at once. I suppose I could use pointers instead of the indexes I have now, but I don't like doing that in Python because half the time it just creates a new object instead of behaving properly.
Wait, what?  That sounds like PHP; Python doesn't create a new object when you assign one to a new name, pass it to a function, or add it to a memory structure.  When are you seeing it create a new object?
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8602 on: December 05, 2015, 07:46:51 pm »

(Please tell me you're not using a while loop and incrementing manually.)
No, I'm just iterating over coordinates (which admittedly is inside a while loop but that's because it varies in how long it takes). The snippet looks bad because... well, it is bad.

...

Wait, what?  That sounds like PHP; Python doesn't create a new object when you assign one to a new name, pass it to a function, or add it to a memory structure.  When are you seeing it create a new object?
I'm... not sure, actually. I could have sworn that it did, but I also don't really know what I'm doing.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8603 on: December 05, 2015, 08:56:56 pm »

I'm... not sure, actually. I could have sworn that it did, but I also don't really know what I'm doing.


That's generally not true. It'll only copy value objects, like integers and characters and floats. It won't make a copy of objects that are any more complicated than that, usually.


It broke my connect 4 AI so many times. Not that it would have worked in the first place. :(

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8604 on: December 05, 2015, 09:44:49 pm »

The copy module, and more specifically copy.copy and copy.deepcopy could be useful for you :v
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8605 on: December 06, 2015, 02:49:44 am »

You'd find that you need more than one thread. I haven't looked into it deeply, but it appears that all gamemaker programs are limited to one thread.

Eh. So are python programs, no big deal.
https://docs.python.org/3.5/library/multiprocessing.html

Or for strict interpretation of thread:
https://docs.python.org/3.5/library/threading.html
https://docs.python.org/3.5/library/_thread.html
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8606 on: December 06, 2015, 03:53:17 pm »

From what I understand, the Global Interpreter Lock (GIL) is a source of many complaints about Python's multi-threading.


It does look to still be in place in 3.5, so it's still slower than it could be.


That being said, it looks like Jython doesn't have this issue, so maybe it would be profitable to look into that if you need multiprocessing.




In any case, it was just an example of how a programming language that doesn't do multithreading well can be useful and widely used. As it turns out, it wasn't a particularly good example.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8607 on: December 06, 2015, 04:00:15 pm »

Python gets around the single thread issue by running multiple processes and communicating between them.  That's actually probably the approach that a lot of scripting languages can or will take, come to think of it.

I guess someone could implement Python without the global interpreter lock, but then you'd have to add extensions to it to provide for multithreading, like thread creation, pooling, events, mutexes and semaphores, and so on...

But in any case, yes, Python is still a very useful language despite its performance issues.
Logged
Through pain, I find wisdom.

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8608 on: December 06, 2015, 06:56:48 pm »

Again, as I was saying, Jython can do just that.

There's also Erlang, which is probably a bit better for interprocess communication, as far as being faster and less prone to breaking.

It's certainly designed for it. Though if you hate functional programming, you probably hate Erlang.

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8609 on: December 07, 2015, 04:28:05 pm »

Python gets around the single thread issue by running multiple processes and communicating between them.  That's actually probably the approach that a lot of scripting languages can or will take, come to think of it.

I guess someone could implement Python without the global interpreter lock, but then you'd have to add extensions to it to provide for multithreading, like thread creation, pooling, events, mutexes and semaphores, and so on...
Many Python programs run multiple processes like that, but not until 2.6 did it get standard library support in the form of the multiprocessing module.  In contrast, the threading module seems to have been added in 1.5.1, offering a simpler interface for the even older thread module.  And no, it's not simple in the slightest to communicate between processes; a general language-level solution would probably perform worse than the GIL.

Meanwhile, Python has been implemented without a global interpreter lock, a few times over.  And yes, thread creation, pooling, events, mutexes, and semaphores are also implemented in each one, as part of the standard library.  The main difference is that destructors are even less predictable, but that's why we have the with block these days.

Even with a GIL, threading makes it convenient to use blocking system calls or long-running C library calls without blocking the event loop, and enables interleaved processing that works on shared memory structures.  It's kind of like running the asyncio loop without leaving yields everywhere.  But yes, it makes Python slower than it should be for CPU-bound work on multi-core machines.

Then again, most programs are I/O-bound, which is why removing the GIL hasn't become a big enough priority for the core developers to overcome the issues that it was designed to solve.
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?
Pages: 1 ... 572 573 [574] 575 576 ... 795