question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize custom_code

See original GitHub issue

It would be awesome to optimize custom_code. Sometimes it works slowly.

Current implementation:

def custom_code(mask: str = '@###',
                char: str = '@', digit: str = '#') -> str:
    code = ''
    for p in mask:
        if p == char:
            code += choice(string.ascii_uppercase)
        elif p == digit:
            code += str(randint(0, 9))
        else:
            code += p
    return code

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lk-geimfaricommented, Dec 18, 2017

@duckyou Not yet! I’ll do it by myself. Thanks!

1reaction
duckyoucommented, Dec 18, 2017

Faster than original implementation:

def _randint(a, b):
    b = b - a
    return int(random.random() * b) + a


def new_custom_code(mask: str = '@###', char: str = '@',
                     digit: str = '#') -> str:

    code = bytearray(
        len(mask)
    )
    mask = mask.encode()
    char = ord(char)
    digit = ord(digit)

    for i, p in enumerate(mask):
        if p == char:
            a = _randint(65, 91)
        elif p == digit:
            a = _randint(48, 58)
        else:
            a = p
        code[i] = a
    return code.decode()

Compare code:

import time
import random
import string


# This function from Fluent Python by Luciano Ramalho.
def clock(func):
    def clocked(*args):
        t0 = time.time()
        result = func(*args)
        elapsed = time.time() - t0
        name = func.__name__
        arg_str = ', '.join(repr(arg) for arg in args)
        print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result))
        return result

    return clocked


def orig_custom_code(mask: str = '@###', char: str = '@',
                 digit: str = '#') -> str:
    code = ''
    for p in mask:
        if p == char:
            code += random.choice(string.ascii_uppercase)
        elif p == digit:
            code += str(random.randint(0, 9))
        else:
            code += p

    return code


def _randint(a, b):
    b = b - a
    return int(random.random() * b) + a


def new_custom_code(mask: str = '@###', char: str = '@',
                     digit: str = '#') -> str:

    code = bytearray(
        len(mask)
    )
    mask = mask.encode()
    char = ord(char)
    digit = ord(digit)

    for i, p in enumerate(mask):
        if p == char:
            a = _randint(65, 91)
        elif p == digit:
            a = _randint(48, 58)
        else:
            a = p
        code[i] = a
    return code.decode()


@clock
def test_orig(count):
    db = [orig_custom_code('@_#_@@##+@_#_@@##+') for _ in range(count)]
    return 'Generated {}'.format(count)


@clock
def test_new(count):
    db = [new_custom_code('@_#_@@##+@_#_@@##+') for _ in range(count)]
    return 'Generated {}'.format(count)


if __name__ == '__main__':
    test_orig(100000)
    test_new(100000)

Results:

[3.20863533s] test_orig(100000) -> 'Generated 100000'
[1.42319679s] test_new(100000) -> 'Generated 100000'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom code in Optimizely Web – Support Help Center
Add custom code to your Optimizely experiments. Optimizely lets you use custom JavaScript or CSS code to make powerful changes to your site...
Read more >
Optimization Strategies - MATLAB & Simulink - MathWorks
Optimize the execution speed or memory usage of generated code. ... To optimize your generated code further, you can: ... Integrate External/Custom Code....
Read more >
Custom Code Guidelines - Website Help
We run PageSpeed optimization on most pages in Website Builder, which can cause issues with your custom code. When troubleshooting code ...
Read more >
Using Standard Functionality vs. Creating Custom Code
Standard objects and declarative features—such as approval processes, flows, and workflow ruless—are highly optimized already and don't count against most ...
Read more >
Custom Code - FT Optimize
Custom Code. We offer custom development work by the hour and no project is out of scope for our team. Having consulted for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found