How to Improve Performance of This Code

How to improve performance of this code?

I've been tripped up by this before too. The bottleneck here is actually if neighbor in closedlist.

The in statement is so easy to use, you forget that it's linear search, and when you're doing linear searches on lists, it can add up fast. What you can do is convert closedlist into a set object. This keeps hashes of its items so the in operator is much more efficient than for lists. However, lists aren't hashable items, so you will have to change your configurations into tuples instead of lists.

If the order of closedlist is crucial to the algorithm, you could use a set for the in operator and keep an parallel list around for your results.

I tried a simple implementation of this including aaronasterling's namedtuple trick and it performed in 0.2 sec for your first example and 2.1 sec for your second, but I haven't tried verifying the results for the second longer one.

How to improve the performance of below code?

This is not my area of expertise but my guess is that there is a lot of overhead in creating a new OleDbConnection for each record that is updated. I could imagine that it at least has to open the access database in the file system, check permissions, parse a bunch of stuff out, etc... An approach where the connection was created once would probably work much better. I also see that there is some Transaction mechanism that could possibly improve performance.

using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\/Topology.accdb"))
{
con.Open();
var trans = con.BeginTransaction();

foreach (var pingReply in replies)
{
var status = "";
if (pingReply.Reply.Status.ToString() == "Success")
{
online++;
status = "Online";
}
else
{
status = "Offline";
offline++;
}

string query = "UPDATE SystemStatus SET SystemStatus=@SystemStatus WHERE IP='" + ipAddr + "'";

OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@SystemStatus", status);

cmd.ExecuteNonQuery();
}
trans.Commit();
}

How to improve the performance of the code and to execute it in less time for VBA EXCEL code COUNTIF

Flag Unique Values

Option Explicit

Sub FlagUniques()

' Reference the worksheet ('ws').
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
' i.e. instead use e.g.
'Set ws = ThisWorkbook.Worksheets("Sheet1")

' Write the header.
ws.Range("BE1") = "Flag_Unico"

' Reference the source (one-column) range.
With ws.Range("BD2:BD" & ws.Cells(ws.Rows.Count, "N").End(xlUp).Row)

' Write the number of rows to a variable ('rCount').
Dim rCount As Long: rCount = .Rows.Count

' Write the values from the source range to the source array ('sData').
Dim sData() As Variant: sData = .Value

' Reference a new dictionary object ('dict').
With CreateObject("Scripting.Dictionary")
.CompareMode = vbTextCompare ' case-insensitive; out-comment if not

' Write the unique values from the source array to the dictionary
' whose 'keys' will hold the unique value while each
' of the corresponding 'items' will hold the count.

Dim r As Long ' Current Row

For r = 1 To rCount
.Item(sData(r, 1)) = .Item(sData(r, 1)) + 1
Next r

' Write the 'True/False' results to the destination array ('dData').

Dim dData() As Boolean: ReDim dData(1 To rCount, 1 To 1)

For r = 1 To rCount
If .Item(sData(r, 1)) = 1 Then ' the count is '1'
dData(r, 1) = True
'Else ' the count is '>1'; the default value is 'False'
End If
Next r

' Or:
' ' Write the '1/0' results to the destination array ('dData').
'
' Dim dData() As Long: ReDim dData(1 To rCount, 1 To 1)
'
' For r = 1 To rCount
' If .Item(sData(r, 1)) = 1 Then ' the count is '1'
' dData(r, 1) = 1
' 'Else ' the count is '>1'; the default value is '0'
' End If
' Next r

End With

' Write the results from the destination array to the destination range.

' Reference the destination (one-column) range.
With .EntireRow.Columns("BE")
' Write.
.Value = dData
' Clear below.
.Resize(ws.Rows.Count - .Row - rCount + 1).Offset(rCount).Clear
End With

End With

' Inform to not wonder if the code has run or not.
MsgBox "Unique values flagged.", vbInformation

End Sub

Improving the performance of C code

In my experience the most unorthodox way of optimizing C code is to profile the application, identify slowly performing structures and or db hits and then design reasonable solutions around them using Big O analysis.

How to improve performance of code you don't own?

  • Get on the newest version of Java (newer versions tend to have performance improvements)

  • Give Java more memory to work with (benchmark to see if this makes any difference)

  • Measure what it's doing with top. Upgrade whatever it's having problems with (more memory, faster CPU, SSD). Some CPUs are better at single threaded work loads than others (read: don't run this on a Bulldozer; something with Turbo Boost might be helpful).

  • Play with other experimental JVM options (benchmark to see if this makes any difference)

  • Remove any other applications running on this machine (benchmark to see if there's any benefit -- no sense wasting money if it doesn't help)

  • Pay the vendor to make it faster or give you the code (ie: give them monetary reasons to fix this)

  • Find an alternative

  • Write your own alternative



Related Topics



Leave a reply



Submit