C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Is there any way to reduce time and code for this code

By : Chen Ben-Enosh
Date : October 17 2020, 06:10 AM
I hope this helps . If I understand correctly, and correct me if I am wrong, the goal is to find common digits between the two numbers.
If so, an easier way to match common integers is get unique digits from both numbers via a set, and then calculate the intersection between them
code :
``````a = 12345
b = 49085

#Convert both numbers into a set to get unique digits
set_a = set(str(a))
set_b = set(str(b))

#Get common digits between both numbers
common = [int(a) for a in set_a.intersection(set_b)]

print(common)
``````
``````[5, 4]
``````

Share :

## reduce execution time of code

By : okarion
Date : March 29 2020, 07:55 AM
Does that help this works for me..... And it is blazingly fast.... i thank everyone for their help :)
code :
``````Sub assginment_count()
Dim a, i As Long, ii As Long, dic As Object, w, e, s
Dim StartDate As Date, EndDate As Date
Set dic = CreateObject("Scripting.Dictionary")
' use dic as a "mother dictionary" object to store unique "Employee" info.
dic.CompareMode = 1
' set compare mode to case-insensitive.
a = Sheets("temp calc").Cells(1).CurrentRegion.Value
' store whole data in "Temp Calc" to variable "a" to speed up the process.
For i = 2 To UBound(a, 1)
' commence loop from row 2.
If Not dic.exists(a(i, 1)) Then
Set dic(a(i, 1)) = CreateObject("Scripting.Dictionary")
' set child dictionary to each unique "Emp Id"
End If
If Not dic(a(i, 1)).exists(a(i, 3)) Then
Set dic(a(i, 1))(a(i, 3)) = _
CreateObject("Scripting.Dictionary")
' set child child dictionary to each unique "Startdt" to unique "Emp Id"
End If
dic(a(i, 1))(a(i, 3))(a(i, 4)) = dic(a(i, 1))(a(i, 3))(a(i, 4)) + 1
' add 1(count) to a unique set of "Emp Id", "Startdt" and "Finishdt", so that it enables to count as
' different match even if multiple same unique set of "Emp Id", "Startdt" and "Finishdt" appears.
Next
With Sheets("dashboard")
StartDate = .[N1].Value: EndDate = .[N2].Value
With .Range("a3").CurrentRegion.Resize(, .Rows(3).Find("*", , , , xlByRows, xlPrevious).Column)
' finding the data range, cos you have blank column within the data range.
.Columns("q").Resize(.Rows.count - 3, .Columns.count - 16).Offset(3).Value = 0
' initialize the values in result range set to "0".
a = .Value
' store whole data range to an array "a"
For i = 4 To UBound(a, 1)
' commence loop from row 4.
If dic.exists(a(i, 1)) Then
' when mother dictionary finds "Employee"
For Each e In dic(a(i, 1))
' loop each "Startdt"
For Each s In dic(a(i, 1))(e)
' loop corresponding "Finishdt"
If (e <= EndDate) * (s >= StartDate) Then
' when "Startdt" <= EndDate and "Finishdt" >= StartDate
For ii = 17 To UBound(a, 2)
' commence loop from col.Q
If (a(3, ii) >= e) * (s >= a(3, ii)) Then
' when date in the list matches to date between "Startdt" and "Finishdt"
a(i, ii) = a(i, ii) + dic(a(i, 1))(e)(s)
' add its count to corresponding place in array "a"
End If
Next
End If
Next
Next
End If
Next
.Value = a
' dump whole data to a range.
End With
End With
End Sub
``````

## Is there any way to reduce execution time of this code?

By : objconn
Date : March 29 2020, 07:55 AM
I wish this helpful for you I would say go up to sqrt(a). Each time you have a remainder 0, add both the i and a/i. You will need to take care of the corner cases, but this should bring down the time complexity. Depending on how large a is this should be faster. For small values this may actually be slower.

## Need help to reduce the execution time for the code below

By : user2603707
Date : March 29 2020, 07:55 AM
it fixes the issue Welcome to SO.Tried your code with around 2000 rows with around 10% mismatch and it take only few seconds. may be the file have some other issues. However one way to speed up (about 1/2 the time taken in my trial) is to add all the bNotFound cells in a union of range and delete the EntireRow of the range in one shot after finishing the loop.
Changes in Code:
code :
``````Dim Rng As Range    'Add in  Declare section
'
'
'
'
For I = 2 To MaxRowO   'No need to loop backward
'
'
'
'
If bNotFound Then                       ' Only add to Union of ranges
If Rng Is Nothing Then
Set Rng = WSO.Range("A" & I)
Else
Set Rng = Union(Rng, WSO.Range("A" & I))
lDel = lDel + 1
End If
End If
Next I

If Not Rng Is Nothing Then Rng.EntireRow.Delete       ' delete in one shot
``````

## How do i reduce time complexity of this code

By : Patrick Staples
Date : October 24 2020, 06:10 AM
hop of those help? You can use Array.prototype.flat()
Note: All the methods below are just alternative which will have high speed. Your solution has O(n) time-complexity and all the method below have same time-complexity. The time-complexity of merging two arrays is O(n) not O(1)
code :
``````let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = arr.flat();
console.log(objs)``````
``````let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = arr.reduce((ac,a) => ac.concat(a),[])
console.log(objs)``````
``````let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = [].concat.apply([], arr)
console.log(objs)``````

## How can i reduce Execution Time for this code

By : DefendHouston
Date : September 30 2020, 05:00 PM
fixed the issue. Will look into that further contains() of LinkedList has O(n) time at the worst case.
Construct an auxiliary HashSet for this purpose, or invent another way to trace elements which had appeared already.