Python的按位移位真的很慢吗? [英] Is Python bitwise shift really slow?

查看:166
本文介绍了Python的按位移位真的很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须忽略一些东西,但是真的不明白为什么Python代码这么慢...

I must be overlooking something, but really don't see why the Python code is so slow...

对数组中元素在[-1,000,000..1,000,000]范围内的唯一元素进行计数,并使用位向量执行此操作.使用BitSet的Java代码比Python的速度(大约9秒)快50倍.

Counting unique elements in an array where elements are in the range [−1,000,000..1,000,000] and use a bitvector to do this. The Java code, which uses BitSet is about 50 times faster than Python, which takes 9 seconds.

这可能是因为当我初始化bitvector = 0时,Python没有保留足够的内存并且随着位向量的增长需要复制位向量吗?

Is this maybe because when I initialise bitvector = 0 Python doesn't reserve enough memory and the bitvector needs to be copied as it grows?

Python:

def solution(array):
    bitvector = 0
    count = 0
    for element in array:
        # transform -1,000,000 to 0 etc
        element_transformed = element + 1000000
        if bitvector >> element_transformed & 1 == 0:
            count += 1
            bitvector = bitvector | 1 << element_transformed

    return count

测试:

import unittest
import random

from .file1 import solution

class MySolutionTests(unittest.TestCase):
    def test_solution_random_all_unique(self):
        a = random.sample(range(-1000000, 1000001), 100000)
        self.assertEqual(100000, solution(a))

在Java中:

package mypackage;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;


public class MyClass {

    public static int solution(List<Integer> array) {
        BitSet bitvector = new BitSet();
        int count = 0;

        for(int i = 0; i < array.size(); i++) {
            int elementTransformed = array.get(i) + 1000000;
            if(bitvector.get(elementTransformed) != true) {
                count++;
                bitvector.set(elementTransformed, true);
            }
        }
        return count;
    }

    public static void main(String[] args) {
        // TODO code application logic here
    }
}

测试:

package mypackage;

import java.util.ArrayList;
import java.util.Collections;
import org.junit.Test;
import static org.junit.Assert.*;

public class MyClassTest {

    public MyClassTest() {
    }

    @Test
    public void testSolutionLong_RandomAllUnique() {
        ArrayList array = new ArrayList();
        for(int i = -1000000; i < 1000000; i++) {
            array.add(i);
        }
        Collections.shuffle(array);
        assertEquals(100000, MyClass.solution(array.subList(0, 100000)));

    }  
}

推荐答案

只需尝试直接回答您提出的问题.回答Python为什么要花9秒而Java要快50倍,这不是一个简单的问题.在这里,您可以很好地了解先例讨论 Python是否比Java/C#慢?

Just trying to reply directly to the question you posed. It is not a simple question to answer why Python takes 9 seconds and Java is 50 times faster. Here you can get a good insight of a precedent discussion Is Python slower than Java/C#?

我喜欢看它的方式是Java是一种面向对象的语言,而python是基于对象的.

The way I like to look at it, is that Java is a Object Oriented language, while python is Object Based.

当查看按位操作时,Java使用原始数据类型,由于没有装箱/拆箱操作和包装器作为抽象层,因此可以说是较快的.因此,在每次迭代时查看您的代码,python都会将整数重新包装为整数类型的对象,而Java不会.

When looking at a bitwise operation, Java uses the primitive data types that are arguably faster due to not having boxing-unboxing operations and wrappers as a layer of abstraction. So looking at your code at every iteration python re-wrappes the integer as an object of type integer, while Java does not.

但是同样,我不会认为Java总是比Python快.取决于您使用的是哪个库以及要解决的问题!

But again, I wouldn't take for granted that Java is always faster than Python. It is up to which library you are using and which problem you are trying to solve!

这篇关于Python的按位移位真的很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆