Asked  6 Months ago    Answers:  5   Viewed   38 times

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about

for i in range(0, 20):
for i in xrange(0, 20):

 Answers

19

In Python 2.x:

  • range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.

  • xrange is a sequence object that evaluates lazily.

In Python 3:

  • range does the equivalent of Python 2's xrange. To get the list, you have to explicitly use list(range(...)).
  • xrange no longer exists.
Tuesday, June 1, 2021
 
Wilk
answered 6 Months ago
88

The current API is that:

  • flatten always returns a copy.
  • ravel returns a view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel will often be faster since no memory is copied, but you have to be more careful about modifying the array it returns.
  • reshape((-1,)) gets a view whenever the strides of the array allow it even if that means you don't always get a contiguous array.
Saturday, June 5, 2021
 
JakeGR
answered 6 Months ago
84

First off, there doesn't seem to be a consensus definition for inline functions in JavaScript. I consider an inline function to be a special case of a JavaScript function. An inline function is a function assigned to a variable that is created at runtime instead of at parsetime.

Anonymous functions and inline functions are practically the same, in that they are created at runtime. The difference is that an inline function is assigned to a variable and so it can be reused. In that way, inline functions work the same as a regular function.

Pre es6, anonymous and inline functions were declared similarly to regular functions using the function keyword. With the advent of es6, anonymous and inline functions could also be declared with the more compact, arrow function syntax.

Function

function func() {
    alert ('function');
} 
$('a').click(func);

Inline Function

var func = function() { 
    alert ('inline') 
};
$('a').click(func);

// Alternative es6+ inline arrow function.
let func2 = () => alert('inline');
$('a').click(func2);

Anonymous Function

$('a').click(function() {
    alert('anonymous');
});
// Alternative es6+ anonymous arrow function.
$('a').click(() => alert('anonymous'));
Friday, July 16, 2021
 
Camsoft
answered 5 Months ago
47
first, rest = l[0], l[1:]

Basically the same, except that it's a oneliner. Tuple assigment rocks.

This is a bit longer and less obvious, but generalized for all iterables (instead of being restricted to sliceables):

i = iter(l)
first = next(i) # i.next() in older versions
rest = list(i)
Wednesday, August 4, 2021
 
turson
answered 4 Months ago
87

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object can then be transmitted or stored, and later reconstructed to create a new object with the same characteristics.

The cPickle module implements the same algorithm, in C instead of Python. It is many times faster than the Python implementation, but does not allow the user to subclass from Pickle. If subclassing is not important for your use, you probably want to use cPickle.

Source of above information.

Monday, September 13, 2021
 
waylaidwanderer
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share