Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 145 146 [147] 148 149 ... 796

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

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2190 on: April 16, 2012, 08:24:26 pm »

Did you try it lots of times?

I agree with Ruler; probably floating point is the norm, and 8-bit integers have to (1) be cut from 64->8 bit (2) throw away everything after the float.

You're a 64-bit system, right?
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

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2191 on: April 16, 2012, 08:31:17 pm »

Hmmm. Maybe downcasting to a 8-bit integer takes some time, or possibly the second scenario is better positioned to take advantage of certain features of modern CPUs?

You misunderstood me. But I misunderstood myself: boost gives me a 32 bit integer between 0 and 255 which is casted to an 8-bit integer. But that 0.5 second difference is still strange. I've been trying to improve the performance of generating random numbers just for fun, but when something like this happens it makes my head hurt. Maybe I could extract four 8-bit numbers from a 32-bit number using bit shifting or something?
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2192 on: April 16, 2012, 08:31:54 pm »

If you are using int's, that is more than an 8-bit integer. chars are 8 bits. int's are 4 bytes, so 32 bits. Also, the compiler is smarter than you think. It's probably optimizing out the 64-bit float part and just doing it straight to 32-bit floats. So, really, you're generating a bunch of strings of 32 bits, and then generating a bunch of strings of 32 bits. The slight discrepancy in time can be attributed to other processes borrowing a bit of the CPU.

kaenneth

  • Bay Watcher
  • Catching fish
    • View Profile
    • Terrible Web Site
Re: if self.isCoder(): post() #Programming Thread
« Reply #2193 on: April 16, 2012, 08:34:59 pm »

I would guess that the random shuffler takes the bulk of the time; everything else is insignificant.

Yeah, taking 64 random bits and doing

a[0] = r || 255;
a[1] = r << 8 || 255;
a[2] = r << 16 || 255;
a[3] = r << 24 || 255;
a[4] = r << 32 || 255;
a[5] = r << 40 || 255;
a[6] = r << 48 || 255;
a[7] = r << 56 || 255;

would be quicker (I might have the << reversed)
Logged
Quote from: Karnewarrior
Jeeze. Any time I want to be sigged I may as well just post in this thread.
Quote from: Darvi
That is an application of trigonometry that never occurred to me.
Quote from: PTTG??
I'm getting cake.
Don't tell anyone that you can see their shadows. If they hear you telling anyone, if you let them know that you know of them, they will get you.

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2194 on: April 16, 2012, 09:19:03 pm »

Your code is completely wrong. However, it got me going in the right direction and I came up with this:

a[0] = r & 255;
a[1] = r >> 8 & 255;
a[2] = r >> 16 & 255;
a[3] = r >> 24 & 255;

It splits a 32-bit integer into four 8-bit integers. It reduced the time from 8.0 seconds down to 7.5 seconds. Woohoo!
Logged

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2195 on: April 16, 2012, 09:33:52 pm »

If you're concerned about speed, you might want to find an alternative to generating 16 million random numbers.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2196 on: April 16, 2012, 09:51:53 pm »

Apparently I was using a slow floating point distribution. (boost reference) I switched from uniform_real_distribution to uniform_01, and now I got the floating point benchmark down to 4.7 seconds. Using 8-bit integers is still 7.5 seconds. :S


EDIT: I got the 8-bit part down to 1.25 seconds! Apparently, passing a byte array through a pointed function 17 million times is slower than using four static byte variables. I have no idea what I'm doing.

So yeah, I can now generate a 4096x4096 random noise image in 1.25 seconds. Awesome. (It takes a few seconds to save the compressed .png to disk, though.)
« Last Edit: April 16, 2012, 10:12:45 pm by dreadmullet »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2197 on: April 17, 2012, 02:25:41 am »

Quote
The challenge lies within concepts, abstract stuff, and connecting all that stuff in your brain, before you write it down.
Quite similar to natural languages, if you ask me. The basic grammar takes only a few months for me to get--but it's the deeper things like idioms, slang, informal grammar, and the like, that are the truly hard part to learn.
Ah, but you can still write a great book without them, just as you can program great things without the deeper stuff of most languages (even if they will make it easier to express yourself, once you know them).


Other question: Anyone know of a really good Database Modeling tool? If it spits out sql, it'd be great but it's no prerequisite. Must work on browser or mac, I just need the graphics.

Edit: Nvm, found a great one for my purposes: wwwsqldesigner.
« Last Edit: April 17, 2012, 03:09:50 am by Siquo »
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Akura

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2198 on: April 17, 2012, 12:44:47 pm »

Not sure if it counts for this thread, but...

A while back I programmed a thing for RPGToolkit that types out each letter of a message as opposed to the normal behavior of writing the whole line using the built-in functions. I went back and looked at it again over the weekend, and noticed it was doing something odd to the first letters of a given string, probably because it was redrawing the same text over itself over and over. Specifically, it redraws the entire line, adding the next letter to the string.

Then I figured I could just write one letter at a time. That didn't work, all the text just jumbled together. I figure I could fix the original problem by clearing the image(why I didn't do that to begin with I have no idea, it seems so obvious) before redrawing it.

I haven't tried it yet, so odds are it's going to screw up horribly like it usually does.
Logged
Quote
They asked me how well I understood theoretical physics. I told them I had a theoretical degree in physics. They said welcome aboard.
... Yes, the hugs are for everyone.  No stabbing, though.  Just hugs.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2199 on: April 17, 2012, 03:12:47 pm »

Two simple questions (bear in mind I'm a C++ newb >_<)

(i) Can a structure hold a structure? As in:

 
Code: [Select]
(There is another structure called 'eexample')
  struct xample
    {
       eexample oranges;
       int apples;
       float cherries;
     };

(ii) How do I create variables, then name them sequentially?

The only way I've thought of is making nodes that hold (a) the 'variable' name, (b) the actual content of the 'variable' and (c) the next node's address.



If the above two are doable, I shall start on my largish console-based D&D combat simulator xD

Another optional, but not necessary, thing to know would be how to access big files with lots of info in them, which would let me both (a) reduce .exe file size and (b) allow users to edit the files.

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

Sowelu

  • Bay Watcher
  • I am offishially a penguin.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2200 on: April 17, 2012, 03:16:32 pm »

Yes, a struct can hold a struct.  Or a pointer to a struct, if that's your thing.

As for naming variables sequentially, bad idea--how would you access them?  Consider using a list vector, or...whatever passes for a dictionary in C++.  I've been so totally spoiled by C#.

Two simple questions (bear in mind I'm a C++ newb >_<)

(i) Can a structure hold a structure? As in:

 
Code: [Select]
(There is another structure called 'eexample')
  struct xample
    {
       eexample oranges;
       int apples;
       float cherries;
     };

(ii) How do I create variables, then name them sequentially?

The only way I've thought of is making nodes that hold (a) the 'variable' name, (b) the actual content of the 'variable' and (c) the next node's address.



If the above two are doable, I shall start on my largish console-based D&D combat simulator xD

Another optional, but not necessary, thing to know would be how to access big files with lots of info in them, which would let me both (a) reduce .exe file size and (b) allow users to edit the files.
Logged
Some things were made for one thing, for me / that one thing is the sea~
His servers are going to be powered by goat blood and moonlight.
Oh, a biomass/24 hour solar facility. How green!

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2201 on: April 17, 2012, 03:20:07 pm »

At naming variables sequentially : I don't know those things is the problem xD What's a vector? Is it those line things in math where planes fly and stuff? No? D:

Uh ...

As for the node list thing method, I was planning on accessing every single node in that list every single time it is called for, cout'ing the names of the list, and then when the user chooses one, going to that variable and accessing the memory~ xD



Random question :

  What happens if I do new int = 8 and then don't assign it anywhere? o_O Does it get lost in memory?

(Again, C++.)
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

Sowelu

  • Bay Watcher
  • I am offishially a penguin.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2202 on: April 17, 2012, 03:24:52 pm »

At naming variables sequentially : I don't know those things is the problem xD What's a vector? Is it those line things in math where planes fly and stuff? No? D:
I believe that lists in C++ are called vectors in the standard library.  It's just, like...well, it's a list of things.

As for the node list thing method, I was planning on accessing every single node in that list every single time it is called for, cout'ing the names of the list, and then when the user chooses one, going to that variable and accessing the memory~ xD
Yeah, that's a job for a dictionary or two lists.  In C# this would be beyond trivial.  In C++, you'd iterate through a 'name' list and print them all out.  Then, when the user selects one by name, iterate through the 'name' list again until you find the index of that string.  Then index into the 'value' list and return the value at that position.  So, if the name they typed was the third element in the name list, you bring up the third element in the value list.  You have to keep things in order; the first element of the name list is always associated with the first element of the value list, etc.

If your list will always be a constant size, just use an array instead.

  What happens if I do new int = 8 and then don't assign it anywhere? o_O Does it get lost in memory?
Yes, AND you get a memory leak.  You should always delete what you free.  Do not call up what you cannot put down.
« Last Edit: April 17, 2012, 03:29:24 pm by Sowelu »
Logged
Some things were made for one thing, for me / that one thing is the sea~
His servers are going to be powered by goat blood and moonlight.
Oh, a biomass/24 hour solar facility. How green!

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2203 on: April 17, 2012, 03:26:41 pm »

In C# this would be beyond trivial.  In C++, you'd iterate through a 'name' list and print them all out.  Then, when the user selects one by name, iterate through the 'name' list again until you find the index of that string.  Then index through the 'value' list and return the value they wanted.  You have to keep things in order; the first element of the name list is always associated with the first element of the value list, etc.

I guess I'll have to do it that way, with nodes. Good thing nodes are basically chained structures >.>

Any words on file I/O as a database?
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

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2204 on: April 17, 2012, 05:06:44 pm »

In C# this would be beyond trivial.  In C++, you'd iterate through a 'name' list and print them all out.  Then, when the user selects one by name, iterate through the 'name' list again until you find the index of that string.  Then index through the 'value' list and return the value they wanted.  You have to keep things in order; the first element of the name list is always associated with the first element of the value list, etc.

I guess I'll have to do it that way, with nodes. Good thing nodes are basically chained structures >.>

Any words on file I/O as a database?

There isn't a really easy way to generate sequential variable names without doing some funky things with preprocessor directives that you probably don't need to be doing anyway.

The std::vector class is like an array, only its capacity is not fixed. So, you can continue to add objects to it, so long as there is memory left on your computer. Nowadays, that shouldn't be a problem.

As for dictionaries in C++, look at std::map.
Pages: 1 ... 145 146 [147] 148 149 ... 796