logo
down
shadow

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


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 : facebook icon twitter icon
reduce execution time of code

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?

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

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

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

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.
Related Posts Related Posts :
  • Why does my else statement run when my if statement is true?
  • Data access layer in Python
  • Sum of array in python
  • How to upgrade sqlite 3.8.2 to >= 3.8.3
  • Python child class with more methods
  • Remove duplicates from list of strings
  • How to remove lowercase sentence fragments from text?
  • How can I use Python with Mechanize for posting multipart/form-data?
  • How to print a range with decimal points in Python?
  • Where to find a list of all the possible HTML tags in Python?
  • Set serial port pin high using python
  • How to recovery source python code (.py) from .pyo file?
  • Python Script to backup a directory
  • return to first function() at the end of last function()
  • Why am I getting output for -2//4 as -1?
  • I want output None instead of 0
  • Issues running python scripts in Command Prompt (Specifically with command line arguments)?
  • How would I start integrating pyflakes with Hudson
  • Name some non-trivial sites written using IronPython & Silverlight
  • How do I do advanced Python hash autovivification?
  • What is the paste deploy uri syntax?
  • Removing duplicates (within a given tolerance) from a Numpy array of vectors
  • How do constructors and destructors work?
  • QTableWidget signal cellChanged(): distinguish between user input and change by routines
  • Reverse Search Best Practices?
  • Any downsides to UPX-ing my 32-bit Python 2.6.4 development environment EXE/PYD/DLL files?
  • Unit Test Won't Run Tests
  • Use two for loops simultaneously
  • can we display glass bar chart in python with google app engine
  • Scapy install issues. Nothing seems to actually be installed?
  • Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?
  • What does s() mean?
  • ROC AUC value is 0
  • Why is this the value?
  • Best practices for logging in django project
  • Is there a python openid apps-discovery library to get appengine apps onto the apps marketplace
  • Cannot fetch a web site with python urllib.urlopen() or any web browser other than Shiretoko
  • Similar to ``tabnanny``, how can I check that all the python code is using 4 spaces as an indent?
  • Python: object identity question?
  • Multiple For loops, print else only once if condition is not met
  • Select one item from Series and keep the index
  • __repr__ method appears can't be invoked automatically for Exception class
  • Problem with list value (ValueError) in python 3
  • How to get TouchSensor nested under joint in Webots (Python API)
  • How to specify kernel while executing a Jupyter notebook using Papermill's Python client?
  • How to hide password in Database Connection?
  • How to get a list of dictionaries from the following code?
  • 'How to find out noun to which pronoun is referring to' in python
  • Removing a character (^) from each row of panda Dataframe and get unique words in each row
  • Changing a static variable of inherited classes
  • Django Query result comparison with if statement
  • Python: how to merge two dataframe based only on different columns?
  • Filter data by last 3 months and by ID
  • Inplace arithmetic operation versus normal arithmetic operation in PyTorch Tensors
  • How can I add custom signs to spaCy's punctuation functionality?
  • Ensure positive difference of two numbers
  • i keep getting an error that my list index is out of range
  • Is there a way to create gantt charts in python?
  • How to view network weights and bias during training
  • How can I force SAS to wait for a command to fully execute?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk