Monday, July 30, 2012 6:40 PM
The following piece of code is used to quickly increment an IPAddress (IPv4) how ever now that Microsoft has implement IPv6 all the usefullness of the IPAddress class has gone.
ip.Address += 0x01000000 // Advance to next IP
What would be the better alternative for high speed incrementing without having to convert, create or re-create an instance as optimization came from the code we must keep the speeds up and so far converting and recreating are extremely costly in comparison and we cant find anything better using search engines.
Monday, July 30, 2012 7:43 PM
If this is adversly affecting your application, there must be something wrong with your code or you are doing something unusual. The following code creates a new IPAddress in 40 nanoseconds. Assuming the obsolete code got a new address in 4 nanoseconds, what are you doing that the difference significantly impacted the performance of your application?
Imports System.Net Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim SW As Stopwatch = Stopwatch.StartNew Dim Int64Adr As Int64 Dim IpAdr As IPAddress For I As Integer = 0 To 1000000 IpAdr = New IPAddress(Int64Adr) Int64Adr += 1 Next SW.Stop() Console.WriteLine(SW.ElapsedMilliseconds) End Sub End Class
Monday, July 30, 2012 7:46 PM
Obviously you cannot do much about that, short of using the deprecated property or, when it's no longer supported, go with an unsafe block and a pointer. I'll just throw two considerations in the mix here:
1) the IPAddress(int) constructor is extremely lightweight. I would be surprised if it had an impact on the performance of your application.
2) assuming that your application uses those addresses to do something useful, I'd wager that any networking operation is going to be orders of magnitude more expensive than creating the new address, however convoluted that process might be. Beware of microoptimizations...
Monday, July 30, 2012 9:59 PM
I tried the obsolete code:
Imports System.Net Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim SW As Stopwatch = Stopwatch.StartNew Dim IpAdr As New IPAddress(0) For I As Integer = 0 To 1000000 IpAdr.Address += 1 Next SW.Stop() Console.WriteLine(SW.ElapsedMilliseconds) End Sub End ClassIt takes 20 nanoseconds. Are you sure the 20 nanoseconds difference significantly impacts your application?