I've lost track of how many times I've seen code that uses a dynamically-expanding array when it could've easily calculated the correct size and allocated exactly the right amount. See: string appending, tokenising, just about everything written in JavaScript, etc.
No wonder software is so slow and bloated...
Name:
Anonymous2015-05-02 16:14
Le Nozze di Figaro : The Marriage of Figaro Opera buffa in quattro atti : Comic opera in four acts Wolfgang Amadeus Mozart (1756 - 1791) Libretto: Lorenzo da Ponte (1749 – 1838) First performed: Vienna, Burgtheater 1st May 1786
DRAMATIS PERSONÆ:
COUNT ALMAVIVA – baritone. COUNTESS ALMAVIVA – soprano, previously Rosina, ward of BARTOLO. SUSANNA – soprano, the COUNTESS’ chambermaid, engaged to FIGARO. FIGARO - bass, the COUNT’s manservant, previously the Barber of Seville, engaged to SUSANNA. CHERUBINO – mezzo-soprano, in love with everyone. MARCELLINA – mezzo-soprano, in love with FIGARO, previously BARTOLO’s housekeeper. DON BARTOLO – bass, previously Rosina’s guardian. DON BASILIO - tenor, previously music teacher employed by BARTOLO to teach COUNTESS, now the COUNT’s middleman for his various romantic affairs. DON CURZIO - tenor, a judge. BARBARINA – soprano, ANTONIO’s daughter and SUSANNA’s cousin, in love with CHERUBINO. ANTONIO - bass, gardener in the COUNT’s gardens, Barbarina's father, Susanna's uncle. Two young girls. Chorus of Peasants.
ATTO PRIMO ACT ONE
Camera non affatto ammobiliata, A partly furnished room - una sedia d'appoggio in mezzo a large sofa centre-stage.
SCENA I SCENE I Figaro con una misura in mano e Figaro has a measuring-stick in hand Susanna allo specchio che si sta and Susanna stands in front of the mirror, mettendo un capellino ornato di trying on a hat decorated with fiori flowers.
N. 1 Duettino No. 1 Duet
FIGARO: FIGARO: [misurando] [measuring] Cinque ... dieci ... venti ... Five ... ten ... twenty ... trenta ... trentasei ... quarantatre. thirty ... thirty-six ... forty-three.
SUSANNA: SUSANNA: [specchiandosi] [looking at herself in the mirror] Ora sì ch'io son contenta; Yes, I'm happy with it now; sembra fatto inver per me. It seems as if it was made for me. Guarda un po', mio caro Figaro, Just look a moment, my dearest Figaro, guarda adesso il mio cappello. look over here at my hat.
FIGARO: FIGARO: Sì mio core, or è più bello, Yes, my heart, it's much prettier now, sembra fatto inver per te. It seems as if it was made for you.
SUSANNA: SUSANNA: Ah, il mattino alle nozze vicino Ah, on the morning of our wedding day quanto è dolce al mio tenero sposo How sweet to my loving bridegroom questo bel cappellino vezzoso is this charming little hat, che Susanna ella stessa si fe'. which Susanna made herself.
FIGARO: FIGARO: Ah, il mattino alle nozze vicino Ah, on the morning of our wedding day quanto è dolce al tuo tenero sposo How sweet to your loving bridegroom questo bel cappellino vezzoso is this charming little hat, che Susanna ella stessa si fe'. which Susanna made herself.
Recitativo Recitative
SUSANNA: SUSANNA: Cosa stai misurando, What are you measuring, caro il mio Figaretto? my dearest Figaro?
FIGARO: FIGARO: Io guardo se quel letto I want to see if the bed che ci destina il Conte which the Count has given us farà buona figura in questo loco. will go well in this spot.
SUSANNA: SUSANNA: E in questa stanza? And in this room?
FIGARO: FIGARO: Certo: a noi la cede Of course: it's been given to us generoso il padrone. by our generous lord and patron.
SUSANNA: SUSANNA: Io per me, te la dono. As for me, I give it to you.
FIGARO: FIGARO: E la ragione? And your reason?
SUSANNA: SUSANNA: [toccandosi la fronte] [tapping her forehead] La ragione l'ho qui. I have my reason here.
FIGARO: FIGARO: [facendo lo stesso] [doing the same] Perché non puoi And why can't you far che passi un po' qui? put it in here?
SUSANNA: SUSANNA: Perché non voglio. Because I don't want to. Sei tu mio servo, o no? Are you my humble servant, or not?
FIGARO: FIGARO: Ma non capisco But I don't understand perché tanto ti spiace why you so dislike la più comoda stanza del palazzo. this most convenient room in the palace.
SUSANNA: SUSANNA: Perch'io son la Susanna, Because I'm Susanna, e tu sei pazzo. and you're mad.
FIGARO: FIGARO: Grazie; non tanti elogi! Thank you; don’t flatter me! Guarda un poco But look - se potriasi star meglio don't you see that we're much better off in altro loco. here than anywhere else?
N. 2 Duettino No. 2 Duet
FIGARO: FIGARO: Se a caso madama If by chance my lady la notte ti chiama, should call you in the night, din din; in due passi 'ding-ding'; in two steps da quella puoi gir. you can be there. Vien poi l'occasione And then, when it happens che vuolmi il padrone, that the Count wants me, don, don; in tre salti 'dong-dong'; in three bounds lo vado a servir. I can go to serve him.
SUSANNA: SUSANNA: Così se il mattino And then, if one morning il caro Contino, the dear little Count, din din; e ti manda 'ding-ding'; should send you tre miglia lontan, three miles away, don don; a mia porta 'dong-dong'; and the devil should bring him il diavol lo porta, here to my door, ed ecco in tre salti ... And then in three steps ...
SUSANNA: SUSANNA: Se udir brami il resto, If you want to hear the rest, discaccia i sospetti Dismiss these suspicions che torto mi fan. that are so unfair to me.
FIGARO: FIGARO: Udir bramo il resto, I must hear the rest, i dubbi, i sospetti these doubts and suspicions gelare mi fan. make my blood run cold.
Name:
Anonymous2015-05-02 17:02
>>1 getline, it even has a extra argument to show you how much extra crap it has allocated.
Name:
Anonymous2015-05-02 17:16
>>1 and how exactly am I supposed to do rapid enterprice apping™ if most of my time is spent obsessing with frivolities such as ``execution speed'', Cudder-sama?
Name:
Anonymous2015-05-02 17:25
I always cringe inside a little when I write code dealing with strings. I have a vague obsessive fear inside of me, that all those use-once little strings that I have inside loops cause constant useless allocations and frees/garbage collection. But the worst is when I concatenate a string from lots of smaller strings and chars. I shudder at the thought that for every call of the concatenation function there's an allocation of a new use-once string happening...
Name:
Anonymous2015-05-02 17:28
// all the memory this program will ever need extern unsigned char mem[MEMSIZE];
Name:
Anonymous2015-05-02 18:08
>>6 #define MEMSIZE 0 you FOOLS! O(1)! O(1) IS WHAT WE NEED!
the TabulaRasa sequence reserves and fills the AI Mind memory:
TabulaRasa: { #2015apr26: print "Size of AI memory is $cns \n"; #2015apr26 my $trc = 0; #2015apr2015 $trc is "tabula rasa counter". until ($trc == $cns) { #2015apr26 PbEx5e p. 193 "Loops". # $aud[$trc] = " "; #2015apr26: Fill CNS with space-32's. # $aud[$trc] = "z"; #2015apr26: Fill CNS w. visible zzzz... $aud[$trc] = " "; #2015apr26: Fill CNS with space-32's. $trc++; #2015apr26: Increment tabula-rasa-counter $trc. } #2015apr26 }; #2015apr26:
Name:
Anonymous2015-05-03 3:25
Look, it's often easier to just call an "add a single item" function in a loop. Don't optimize low-level data structures until you know there's not a lower-hanging fruit.
Name:
Anonymous2015-05-03 9:26
acc = ""; for(s in a) { acc += s } will run in O(n2). You'll feel the difference when it gets longer, though it doesn't always get long enough to be noticeable. You should always use the routine that allocates the final string once and copies all the little string into it. Once you understand it, it isn't any harder than the naive method.
Name:
Cudder !cXCudderUE2015-05-03 13:53
>>11 It's only easy if you don't think ahead. IRL this is like putting your things in one box, finding it's too small to hold everything, then getting a bigger one, taking things out of the old one and putting it into the bigger one, then seeing it still doesn't fit, getting an even bigger one, moving everything into it, ... If you kept doing this, you'd be called a retard before long.
This is absurdly low-hanging fruit. It's not even hanging. It's sitting on the ground.
>>13 Being called a retard often doesn't seem to faze you, though.
Name:
Anonymous2015-05-03 14:13
>>13 Yes, arrays are a shitty datastructure. Breaking news! Hardly anyone uses them these days. There are plenty of efficient, enlargeable purely functional data structures like finger trees.
If you kept doing this, you'd be called a retard before long
Many vectorize implementations will double the size every time more space is needed, so the computational cost scales logarithmically rather than linearly as you add elements. This means you may be wasting up to half of every vector, of course, but if you're only allocating a few short ones you won't care.
>>15 Trees are even worse when you need to process the data sequentially.
>>17-22 I know it's "amortised constant time", but that doesn't say anything about the constant.
Suppose each resize or allocate costs 20 clock cycles (which is extremely low), and a resize costs 1 cycle for each array element copied. You're adding 128 elements, one at a time, to a resizeable array that starts out at 1 element and doubles in size afterwards. Adding each element takes 1 clock cycle. Counting the elements ahead of time takes 10 clock cycles.
If you can figure out how big it's going to get ahead of time, then do that; but if it's data coming off a stream or something else, then it can't be helped.
Name:
Anonymous2015-05-04 6:15
>>23 Cudder-san, 1. B-Trees with forwarding links at the leaves 2. Counting cycles is valid, but... 3. JS is not for microcontrollers 4. I would cry in joy if adding elements took 0 cycles 5. Some resizes may not need copying 6. Serious strings are not byte arrays 7. Imperative foreach instead of functional Array.join() deserves Lv.10 Slow!
Name:
Anonymous2015-05-04 6:25
quadruple the array if its below 10MB , afterwards 1.5x the array size.
Also, couldn't you just stop this obsessive premature micro-optimization everywhere? Maybe that's why your projects are never complete! Maybe you're such a micro-architecture genius that your brain is too comfortable with all this and you can't even see what you're doing! And then when you projects are this big:
you lose more time recomputing your constant resizing definitions! Hey Cudder-san, you don't need to make optimal resource usage in every application, because you are already fine! Maybe you should make perfect use of other resources, like fingers, ice cream and fresh salmon! Maybe you should join Admin-san and help him in not letting progrider board fall to rust [not the language, but I don't like it (too)] too...
Btw my cat bit me and I'm slowly turning into a blueish small ice fairy.
Suppose each resize or allocate costs 20 clock cycles (which is extremely low)
Yeah, I think we would all kill for that kind of performance. It seems like half the world can live with an LLC miss and a couple thousand cycles for a trip through the collection library.
Name:
Anonymous2015-05-04 10:09
>>23 Haskell has lots of packages for processing streams in constant space. Enjoy your imperative world in which