(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
05-18-24 02:05 PM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - Programming tips and tricks New poll | |
Add to favorites | Next newer thread | Next older thread
User Post
Sukasa

Birdo
Not quite as active as before.
Xkeeper supporter
Xk > ||bass
I IP Banned myself! Twice!








Since: 11-17-05
From: Somewhere over there

Last post: 6299 days
Last view: 6298 days
Posted on 09-15-06 09:14 PM Link | Quote
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.
Guy Perfect









Since: 11-18-05

Last post: 6300 days
Last view: 6299 days
Posted on 09-18-06 04:49 PM Link | Quote
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. (-:
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6299 days
Last view: 6299 days
Posted on 09-18-06 05:50 PM Link | Quote
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.
rubixcuber

Mole








Since: 09-08-06
From: St. Louis, MO

Last post: 6408 days
Last view: 6408 days
Posted on 09-19-06 01:39 AM Link | Quote
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!
Guy Perfect









Since: 11-18-05

Last post: 6300 days
Last view: 6299 days
Posted on 09-19-06 01:10 PM Link | Quote
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.
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 6301 days
Last view: 6298 days
Posted on 09-19-06 02:11 PM Link | Quote
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.
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6299 days
Last view: 6299 days
Posted on 09-19-06 03:05 PM Link | Quote
Well when you use a lot of memory, you start having to use virtual memory which is slow.
Ailure

Mr. Shine
I just want peace...








Since: 11-17-05
From: Sweden

Last post: 6299 days
Last view: 6298 days
Posted on 09-19-06 03:24 PM Link | Quote
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? :/
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 6301 days
Last view: 6298 days
Posted on 09-19-06 05:24 PM Link | Quote
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.
Jagori

150


 





Since: 11-17-05

Last post: 6299 days
Last view: 6299 days
Posted on 09-19-06 06:14 PM Link | Quote
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.


(edited by Jagori on 09-19-06 05:14 PM)
never-obsolete

Paragoomba








Since: 05-14-06
From: AZ

Last post: 6300 days
Last view: 6300 days
Posted on 09-19-06 09:34 PM Link | Quote
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.
Guy Perfect









Since: 11-18-05

Last post: 6300 days
Last view: 6299 days
Posted on 09-20-06 02:35 PM Link | Quote
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. (-:
rubixcuber

Mole








Since: 09-08-06
From: St. Louis, MO

Last post: 6408 days
Last view: 6408 days
Posted on 09-21-06 03:34 AM Link | Quote
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.
Add to favorites | Next newer thread | Next older thread
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.010 seconds; used 416.28 kB (max 530.14 kB)