[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-04 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

As Brandt shows us, match-case in its current implementation is not 
significantly different from an if-else ladder in term of performance, though I 
still maintain that match-case is much more readable than an if-else ladder. I 
also agree with Karthikeyan and Terry that global replacement is not a good 
idea, at least not without a significant performance improvement. Therefore I 
will close this issue.

Currently, some people at issue 44283 are working on improving the performance 
of match-case so fingers are crossed. If there is some significant gain in 
performance then I will open new issues on case to case basis.

--
resolution:  -> postponed
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

To me, Raymond's flattening is a plausible replacement, especially ater a 
future speedup.  However, I would re-order the patterns to None, False, True, 
str(), int(), ..., _.  These independent conditions, other than _ (or else in 
the if chain), can be ordered for best reading and comprehension speed, and I 
think the above is an improvement.

Ordering for execution speed would instead start with the most frequently true 
conditions.  An advantage of match over 'if' is that patterns are apparently 
easier to re-order or otherwise automatically optimize.

I agree that replacement should only be done when there is significant 
improvement in reading and/or execution speed.  To avoid backport issues, they 
should best be preceded by a check for bug reports and especially PRs that will 
or do affect the same area of code.

The proposal for a general, +- global replacement has been and will be 
rejected. I think that this issue should therefore be closed, fairly soon, as 
such.

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-02 Thread Brandt Bucher


Brandt Bucher  added the comment:

Hm, that benchmark seems really noisy. Looking at your code, it appears that 
you aren't actually iterating over the results.

I've attached a version of your benchmark rewritten to use pyperf. Here are the 
results on a system with a fresh PGO/LTO build of CPython main, run with CPU 
isolation:

(py) bucher@is-bucher-p1:~/src/patmaperformance$ sudo $(which pyperf) system 
tune > /dev/null
(py) bucher@is-bucher-p1:~/src/patmaperformance$ sudo $(which python) 
if_match.py --rigorous
.
if: Mean +- std dev: 2.16 ms +- 0.13 ms
.
match: Mean +- std dev: 2.13 ms +- 0.07 ms

It appears that there is no significant difference between the two (which is 
what I expect, given the current implementation). With that said, our work in 
issue 44283 will likely have a significant impact for cases like this.

--
Added file: https://bugs.python.org/file50083/if_match.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-02 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

I have used timeit module. I have also attached the test file with this message

--
Added file: https://bugs.python.org/file50081/match_test.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-02 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

How did you do the timing?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-02 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

I have timed the execution of if-else and match-case on Differ().compare from 
difflib module and here is what I got

When both strings are same

**

if-else: 2.504900476196781e-06, match-case: 2.587399649200961e-06, comparisions 
: 10
if-else: 2.222519979113713e-06, match-case: 1.7874199693324045e-06, 
comparisions : 100
if-else: 1.954343999386765e-06, match-case: 1.8695319959078916e-06, 
comparisions : 1000
if-else: 3.2847548005520366e-06, match-case: 1.928162499825703e-06, 
comparisions : 1
if-else: 1.2241538699890953e-06, match-case: 7.870903900038684e-07, 
comparisions : 10
if-else: 7.950048359998618e-07, match-case: 7.883418589990469e-07, comparisions 
: 100
if-else: 7.941918295000505e-07, match-case: 7.882559125006082e-07, comparisions 
: 1000
if-else: 7.928842861700104e-07, match-case: 7.855620772999828e-07, comparisions 
: 1

**

When strings have some difference

**

if-else: 2.7084999601356687e-06, match-case: 2.6756002625916154e-06, 
comparisions : 10
if-else: 2.207159996032715e-06, match-case: 1.8606500088935719e-06, 
comparisions : 100
if-else: 2.139014999556821e-06, match-case: 1.928671001223847e-06, comparisions 
: 1000
if-else: 2.682303600158775e-06, match-case: 2.626289399631787e-06, comparisions 
: 1
if-else: 1.1338948200136655e-06, match-case: 7.989683500636602e-07, 
comparisions : 10
if-else: 7.862168830033624e-07, match-case: 7.83532044995809e-07, comparisions 
: 100
if-else: 7.918311449997419e-07, match-case: 7.843428884996683e-07, comparisions 
: 1000
if-else: 7.843063791000168e-07, match-case: 7.842913352399773e-07, comparisions 
: 1

**

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

In difflib, there's an example where it would be easy to run performance tests.

match tag:
case 'replace':
g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
case 'delete':
g = self._dump('-', a, alo, ahi)
case 'insert':
g = self._dump('+', b, blo, bhi)
case 'equal':
g = self._dump(' ', a, alo, ahi)
case _:
raise ValueError('unknown tag %r' % (tag,))

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

If the json.encoder code does get updated, it doesn't need two levels of 
matching.  It can be flattened by eliminating the *chunks* variable.

match value:
case str():
yield _encoder(value)
case None:
yield 'null'
case True:
yield 'true'
case False:
yield 'false'
case int():
yield _intstr(value)
case float():
yield _floatstr(value)
case list() | tuple():
yield from _iterencode_list(value, _current_indent_level)
case dict():
yield from _iterencode_dict(value, _current_indent_level)
case _:
yield from _iterencode(value, _current_indent_level)

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

I guess we will have to wait until Python 3.10 is released and we have more 
conclusive data about speed and readability of match-case before we can go 
ahead with this issue.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

match-case has not even reached a stable version of Python yet, it is only 
available in Python 3.10 which is still in beta. Are we sure that it is faster 
in all cases and how do you know it is more intuitive when the vast majority of 
Python users have likely not even heard of match-case yet?

Personally, I am looking forward to using match-case, but I can see myself 
having to write lots of comments explaining what the code is doing for many 
years to come.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

This is a relatively simple example of how this will improve readability of the 
code.
(This example is form Lib/json/encoder.py)

Original

-
if isinstance(value, str):
yield _encoder(value)
elif value is None:
yield 'null'
elif value is True:
yield 'true'
elif value is False:
yield 'false'
elif isinstance(value, int):
yield _intstr(value)
elif isinstance(value, float):
yield _floatstr(value)
else:
if isinstance(value, (list, tuple)):
chunks = _iterencode_list(value, _current_indent_level)
elif isinstance(value, dict):
chunks = _iterencode_dict(value, _current_indent_level)
else:
chunks = _iterencode(value, _current_indent_level)
yield from chunks 
--

Suggested

--
match value:
case str():
yield _encoder(value)
case None:
yield 'null'
case True:
yield 'true'
case False:
yield 'false'
case int():
# see comment for int/float in _make_iterencode
yield _intstr(value)
case float():
# see comment for int/float in _make_iterencode
yield _floatstr(value)
case _:
match value:
case list()|tuple():
chunks = _iterencode_list(value, _current_indent_level)
case dict():
chunks = _iterencode_dict(value, _current_indent_level)
case _:
chunks = _iterencode(value, _current_indent_level)
yield from chunks


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-06-01 Thread Kshitiz Arya


Kshitiz Arya  added the comment:

Pardon my ignorance here but can I start working on this issue? I am a new 
contributor therefore I am unsure about how to proceed from here

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-05-31 Thread Brandt Bucher


Change by Brandt Bucher :


--
nosy: +brandtbucher

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-05-31 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

This is done on a case by case basis and in places necessary in future code. 
Modifying existing code can pollute git history, make backports hard, might 
introduce subtle bugs etc. This is similar to proposal to use walrus operator, 
f-strings, etc. throughout the codebase.

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44276] Replace if-elif-else structure with match-case (PEP634)

2021-05-31 Thread Kshitiz Arya


New submission from Kshitiz Arya :

Replace if-elif-else with match-case for pattern matching, which is generally 
faster and more intuitive.

--
components: Library (Lib)
messages: 394839
nosy: Kshitiz17
priority: normal
severity: normal
status: open
title: Replace if-elif-else structure with match-case (PEP634)
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com