(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
06-02-24 01:29 AM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Programming tips and tricks
  
User name:
Password:
Reply:
 
Options: - -
Quik-Attach:
Preview for more options

Max size 1.00 MB, types: png, gif, jpg, txt, zip, rar, tar, gz, 7z, ace, mp3, ogg, mid, ips, bz2, lzh, psd

UserPost
rubixcuber
Posts: 27/356
When you're working in something like embedded assembly code with limited swap space doing a swap with the XOR avoids a load/store and is actually faster than the temporary variable. It just depends what you're doing and what you want to optimize if this method is good for you.
Guy Perfect
Posts: 353/451
Originally posted by neotransotaku
That's completely absurd. With this logic, Windows should run insanely fast and lighter operating systems like FreeBSD are insanely slow.
I didn't say that the more memory a program uses, the faster it is... I said that the faster a program is, the more memory it is likely to use.

The former is an interesting idea. I can just imagine a program loading 500MB of dummy data into RAM just to make itself run faster. (-:
never-obsolete
Posts: 66/79
Originally posted by Ailure

any examples?



in order to finish my NMI hanlder before VBlank ended in an NES demo, i unrolled some loops and made look up tables. this yielded faster execution time at the cost of more used prg-rom space. a few hundred cycles is insignificant on modern processors, but for the NES its enough to eliminate graphical glitches.
Jagori
Posts: 127/155
I think he meant something more along the lines of "optimizing for speed often comes at the cost of added memory usage", not "more memory usage = faster."

As a corollary to that, I've found that if there is a way to optimize for both speed and space, it almost always makes your code freaking hard to read.
neotransotaku
Posts: 1700/1860
If a program (and it's working space) completely fits within 1 page of memory and the operating system never swaps any of its space back to the HD, then that program is superior hands down.

However, I still don't see how a program can run faster if it uses a lot of memory such that it constantly needs the VM system.
Ailure
Posts: 1849/2602
True, but even with no virtual memory used some of the lighter OS I seen in RAM usage tends to also be faster. I can see it being faster incase the program keeps saving and loading from the HD, instead of using the RAM but other than that... any examples? :/
HyperHacker
Posts: 3365/5072
Well when you use a lot of memory, you start having to use virtual memory which is slow.
neotransotaku
Posts: 1699/1860
Originally posted by Guy Perfect
Speed and memory usage tend to be directly proportional. The faster your program is, the more memory it will use. The less memory your program uses, the slower it will be.


That's completely absurd. With this logic, Windows should run insanely fast and lighter operating systems like FreeBSD are insanely slow.
Guy Perfect
Posts: 350/451
Speed and memory usage tend to be directly proportional. The faster your program is, the more memory it will use. The less memory your program uses, the slower it will be.

In your example, rubixcuber, you performed three bitwise operations and three assignment operations to avoid allocating memory for a third variable. Using a third variable, there would only be three assignment operations and the bitwise processing would be omitted altogether. More memory, but more speed.
rubixcuber
Posts: 5/356
How to swap two variables without creating a temporary variable:

If you have two variables A and B which you want to swap

1) A = A XOR B
2) B = B XOR A
3) A = A XOR B

And there you have it!
HyperHacker
Posts: 3353/5072
Originally posted by Guy Perfect
• When working with bytes, use bitwise operators instead of arithmetic operators. For example, saying "GreenBits = Int((ColorValue - Int(ColorValue / 65536) * 65536) / 256)" will return bits 8 to 15 of a number, but you can get the same value by saying "GreenBits = (ColorValue And 65280) / 256"

Even better, use right-shift by 8 instead of divide by 256. (I don't think VB has a right-shift operator, but you can't really talk about optimization in VB. ) Division (and to a lesser extent multiplication) is very slow on most processors, especially those with no FPU (many 2D game consoles) that have to do it manually. Use shifts in place of multiplication/division by powers of two wherever possible.
Guy Perfect
Posts: 348/451
The WinSock controll has an event called SendComplete that will be triggered when the remote client has recieved all the data from a TCP transfer. It would be better to use a global variable that increments every time a SendComplete event occurs.



• The keenest of optimizations is to avoid using abstract, upper-level programming languages. In particular, Microsoft Visual Basic and lately the entire .NET suite are notorious for producing more code than they need to. For best results, stick with the older stuff like C (not to be confused with C++) or, if it REALLY matters, straight assembly for your system's processor.

• Generally, subtraction is faster than addition because it removes the need to perform an overflow check when used on positive numbers. So with For loops, saying "0 to 9 by 1" can be optimized to say "9 to 0 by -1"

• Less code does not necessarily mean faster code. Saying "A = B + C / D" twelve times will be faster than calling a function 12 times to do the same thing. Many languages allow you to code macroes using some kind of #define directive which allows you to program with literal code as if it was a function.

• Simple code as well does not mean faster code. Adding all the positive integers from 1 to 100 will yield 5050, but the result can also be expressed as "(1 + 100) * (100 / 2)," where you can modify that to allow for any lower- or upper-bound values. Algorithmically, complicated expressions can be MUCH faster than simple ones.

• For best results, avoid the use of classes. Using memory structures with a set of procedures to manipulate them requires less memory and overhead; thusly improving the speed of the program's execution. There's not much that you can do with classes that you can't do with user-defined structures; and what you can usually involves layers of more complicated processing.

• File access from hard disk is a very slow procedure. To optimize processing speed, load much (preferably all) of a file's data into memory at one time then access it from there. The same goes for writing data to disk; get all the data ready in RAM, then write it all at once.

• When working with bytes, use bitwise operators instead of arithmetic operators. For example, saying "GreenBits = Int((ColorValue - Int(ColorValue / 65536) * 65536) / 256)" will return bits 8 to 15 of a number, but you can get the same value by saying "GreenBits = (ColorValue And 65280) / 256"



There's plenty more, but I don't feel like typing up any more. (-:
Sukasa
Posts: 1688/2068
I had a bright idea! This is it- a thread where if you have a little trick or optimization, or even soemhting that seems obvious but is really easy to miss, post it here for others to use if they need to... Here's one ot start you off.

OK... I had a small problem with the winsock control is visual basic 6, where I had the code bit

for a = 0 to 31
if TCPclientport(a).state = SckConnected then
TCPclientport.senddata data
end if
next a

The probem was that only the user on the highest connected port number got the message... I fixed the problem by changing the code to this:

for a = 0 to 31
if TCPclientport(a).state = SckConnected then
TCPclientport.senddata data
DoEvents 'allows Windows to actually send the data
end if
next a

this fixed the problem. helpful if you're using a set of controls and are trying to broadcast data to a multitude of people at once.
Acmlm's Board - I3 Archive - Programming - Programming tips and tricks


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.013 seconds; used 360.49 kB (max 420.13 kB)