Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT Use list comprehension #12883

Merged
merged 5 commits into from Nov 26, 2018
Merged

Conversation

rth
Copy link
Contributor

@rth rth commented Nov 25, 2018

This adds a few list comprehensions in places where it doesn't hurt readability, which should be somewhat faster and less verbose.

return segments, codes_list
segments, codes_list = zip(
*[path_to_3d_segment_with_codes(path, pathz, zdir)
for path, pathz in zip(paths, zs)])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #11577, this should be faster,

In [4]: sequence = list(zip(range(1000), range(1000)))                                                                                                       

In [5]: %%timeit 
   ...: x = [] 
   ...: y = [] 
   ...: for x_el, y_el in sequence: 
   ...:     x.append(x_el) 
   ...:     y.append(y_el) 
   ...:                                                                                                                                                      
135 µs ± 498 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [6]: %%timeit 
   ...: x, y = zip(*sequence) 
   ...: x = list(x) 
   ...: y = list(y) 
   ...:  
   ...:                                                                                                                                                      
49.2 µs ± 234 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a good performance improvement! However, it gets hard to read because there's too much going on. I propose to construct the list beforehand and give it a name. This should be equally fast.

    segments_and_codes_list = [
        path_to_3d_segment_with_codes(path, pathz, zdir)
        for path, pathz in zip(paths, zs)
    ]
    segments, codes = zip(*segments_and_codes_list)
    return list(segments), list(codes)

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution and welcome to Matplotlib!

These are all very good changes and I only have some minor comments.

lib/matplotlib/collections.py Outdated Show resolved Hide resolved
l = Line2D([thisx, thisx], [bottom, thisy])
leg_stemlines.append(l)
leg_stemlines = [Line2D([thisx, thisx], [bottom, thisy])
for thisx, thisy in zip(xdata_marker, ydata)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "this" prefix doesn't add any information and just makes it harder to read. While you're at it, can you please change this to for x, y in ...? That would further improve the code.

return segments, codes_list
segments, codes_list = zip(
*[path_to_3d_segment_with_codes(path, pathz, zdir)
for path, pathz in zip(paths, zs)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a good performance improvement! However, it gets hard to read because there's too much going on. I propose to construct the list beforehand and give it a name. This should be equally fast.

    segments_and_codes_list = [
        path_to_3d_segment_with_codes(path, pathz, zdir)
        for path, pathz in zip(paths, zs)
    ]
    segments, codes = zip(*segments_and_codes_list)
    return list(segments), list(codes)

@rth
Copy link
Contributor Author

rth commented Nov 25, 2018

Thanks for the review @timhoffm ! Addressed your comments..

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks Good.

@anntzer
Copy link
Contributor

anntzer commented Nov 26, 2018

Thanks for you contribution, hoping to see you around again!

@anntzer anntzer added this to the v3.1 milestone Nov 26, 2018
@anntzer anntzer merged commit 6d91ed9 into matplotlib:master Nov 26, 2018
@rth rth deleted the list-comprehension branch November 26, 2018 08:47
Copy link
Member

@WeatherGod WeatherGod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a bit late, but this PR introduced a potential bug.

seg.append((x, y, z))
codes.append(code)
seg_codes = [((x, y, z), code) for ((x, y), code), z in zip(pathsegs, zs)]
seg, codes = zip(*seg_codes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be protected by an empty-check, because it'll fail if pathsegs or zs is empty. With the previous implementation, you got back empty lists.

return segments, codes_list
segments_codes = [path_to_3d_segment_with_codes(path, pathz, zdir)
for path, pathz in zip(paths, zs)]
segments, codes = zip(*segments_codes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be protected by an empty-check.

@rth
Copy link
Contributor Author

rth commented Nov 26, 2018

Thanks @WeatherGod , I forgot about the same comment by you from #11577 (comment) indeed.

Will do a follow up PR with a fix.

@rth
Copy link
Contributor Author

rth commented Dec 3, 2018

Made a PR to fix the added the zip unpacking issue in #12927

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants