Skip to content

Commit

Permalink
Added starter content to performance to clear CI check.
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyG committed Jul 29, 2023
1 parent 395736c commit 6173194
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
30 changes: 30 additions & 0 deletions exercises/practice/acronym/.articles/performance/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Performance

In this approach, we'll find out how to most efficiently form an acronym from an input string.

The [approaches page][approaches] lists seven idiomatic approaches to this exercise:

1. [Using a `list-comprehension`][approach-list-comprehension]
2. [Using a `loop`][approach-loop]
3. [Using `functools.reduce()`][approach-functools-reduce]
4. [Using `map()`][approach-map-function]
5. [Using a `generator-expression`][approach-generator-expression]
6. [Using a `regex` with `str.join()`][approach-regex-join]
7. [Using `re.sub()`][approach-regex-sub]


## Benchmarks

To benchmark these approaches, we wrote a [small benchmark application][benchmark-application] using [].
The benchmark checks the various approaches against
Besides the regular CPU-time columns, the amount of memory used was also tracked.

These are the results:

[approach-functools-reduce]: https://exercism.org/tracks/python/exercises/acronym/approaches/functools-reduce
[approach-generator-expression]: https://exercism.org/tracks/python/exercises/acronym/approaches/generator-expression
[approach-list-comprehension]: https://exercism.org/tracks/python/exercises/acronym/approaches/list-comprehension
[approach-loop]: https://exercism.org/tracks/python/exercises/acronym/approaches/loop
[approach-map-function]: https://exercism.org/tracks/python/exercises/acronym/approaches/map-function
[approach-regex-join]: https://exercism.org/tracks/python/exercises/acronym/approaches/regex-join
[approach-regex-sub]: https://exercism.org/tracks/python/exercises/acronym/approaches/regex-sub
29 changes: 18 additions & 11 deletions exercises/practice/acronym/approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ There are multiple Pythonic ways to solve the Acronym exercise.
Among them are:

- Using `str.replace()` to scrub, and a `for loop` with string concatenation via the `+` operator.
- Using `str.replace()` to scrub, and joining via `str.join()`passing a `list-comprehension` or `generator-expression`.
- Using `str.replace()` to scrub, and joining via `map()` or `functools.reduce()`.
- Using `re.findall`/`re.finditer` to scrub, and `str.join` with a `generator-expression` or `list-comprehension`.
- Using only `re.sub` (aka "only" regex)`
- Using `str.replace()` to scrub, and joining via `str.join()`passing a `list-comprehension`
- Using `str.replace()` to scrub, and joining via `str.join()`passing a `generator-expression`.
- Using `str.replace()` to scrub, and joining via `functools.reduce()`.
- Using `str.replace()` to scrub, and joining via `str.join()` passing `map()`.
- Using `re.findall()`/`re.finditer()` to scrub, and `str.join()` with a `generator-expression`.
- Using `re.sub()` (_using "only" regex_)`


## General Guidance

The goal of the Acronym exercise is to collect the first letters of each word in the input phrase and return them as a single capitalized string (_the acronym_) .
The goal of the Acronym exercise is to collect the first letters of each word in the input phrase and return them as a single capitalized string (_the acronym_).
The challenge is to efficiently identify and capitalize the first letters while removing or ignoring non-letter characters such as `'`,`-`,`_`, and white space.

Strings are _immutable_, so any method to produce an acronym will be creating a new `str`.

There are two idiomatic strategies for non-letter character removal:
- Python's built-in [`str.replace()`][str-replace].
- [`re`][re] module, (_regular expressions_).

Forming an acronym is most easily done with a direct or indirect loops, although some regex methods can avoid looping constructs altogether.
For all but the most complex scenarios, using `str.replace()` is generally more efficient than using a regex.

The challenge is to efficiently identify and capitalize the first letters while removing or ignoring non-letter characters such as `'`,`-`,`_`, and white space.
Forming the final acronym is most easily done with a direct or indirect loop, after splitting the input into a word list via [`str.split()`][str-split].
Some `regex` methods can avoid looping altogether, although they can become very non-performant due to backtracking.

Strings are _immutable_, so any method to produce an acronym will be creating and returning a new `str`.


## Approach: scrub with `replace()` and join via `for` loop
Expand Down Expand Up @@ -164,11 +171,11 @@ However, these listed approaches cover the majority of 'mainstream' strategies.

## Which approach to use?

All seven approaches are idiomatic, and show multiple paradigms and possiblities.
All seven approaches are idiomatic, and show multiple paradigms and possibilities.

The `list-comprehension` approach is the fastest, although `loop`, `map`, and`reduce`near identical in performance.
The `list-comprehension` approach is the fastest, although `loop`, `map`, and `reduce` have near-identical performance for the test data.

The least performant for the input data was using a `generator-expression` , `re.findall` and `re.sub` (least performant).
The least performant for the test data was using a `generator-expression`, `re.findall` and `re.sub` (least performant).

To compare performance of the approaches, take a look at the [Performance article][article-performance].

Expand Down

0 comments on commit 6173194

Please sign in to comment.