Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4594754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:52:23+00:00 2026-05-21T22:52:23+00:00

I just wrote this code, and I was wondering why it performs significantly worse

  • 0

I just wrote this code, and I was wondering why it performs significantly worse in Python 3? Is it the same across all platforms? Is this just bad luck, or is Py3 generally slower?

Thanks!

Performance:

       python 2.6  python 3.1  pypy 1.5
linux  2.2s        2.4s        0.8s
os x   2.5s        3.4s        0.7s

Code: (sorry it’s so hasty and inefficient!)

import itertools
import random

def fptp_draw(result):
    votes = [prefs[0] for prefs in result]
    counts = [len([v for v in votes if v == c]) for c in [1, 2, 3]]
    s = sorted(counts)
    #print('fptp', counts)
    return s[-1] == s[-2]

def av_remove(prefs, cand):
    if prefs[0] != cand:
        return prefs
    else:
        return prefs[1:]

def av_draw(result):
    nv = len(result)
    cands = [1, 2, 3]
    while True:
        votes = [prefs[0] for prefs in result]
        counts = [len([v for v in votes if v == c]) for c in cands]
        #print('av  ', cands, counts)
        s = sorted(counts)

        if s[-1]*2 > nv:
            return False
        if len(cands) == 2:
            return True

        loser = cands[counts.index(s[0])]
        cands.remove(loser)

        result = [av_remove(prefs, loser) for prefs in result]

    return False

#orders = list(itertools.permutations([1, 2, 3]))
orders = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

#results = list(itertools.product(*itertools.repeat(orders, 6)))
#results = random.sample(results, 5)

def rand_vote():
    return [random.choice(orders) for i in range(1000)]

n = fptp = av = 0
for j in range(1000):
    r = rand_vote()
    #print()
    #print(r)
    n += 1
    if fptp_draw(r):
        fptp += 1
    if av_draw(r):
        av += 1

print(fptp*100.0/n, av*100.0/n)
print(n)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:52:23+00:00Added an answer on May 21, 2026 at 10:52 pm

    Py3k is generally slower than python 2.x. That will change as time passes, but the focus for py3k was feature completeness and stability, not speed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote this... horrible line of Ruby code, and I'm wondering if there
I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
This is just a question on how to write my code for a COM
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I wrote a program to solve a complicated problem. This program is just a
I just wrote some code to test the behavior of std::equal, and came away
This is some kind of academic question, I just wondering if it is possible
I am just wondering if I'm doign something that might be bad, although it
I was just wondering if this approach looks like a good practice for apps
I'm profiling code of a game I wrote and I'm wondering how it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.