Redis SELECT性能(Redis SELECT performance)
我正在使用带有多个数据库的redis(我通过SELECT命令切换)。
我将不同类型的信息存储到redis中,我需要以某种方式将其分开。 我不喜欢在密钥的前面添加信息类型,因此我创建了更多的数据库。
我想问一下这是一个正确的决定,关注绩效吗?
SELECT还会产生多少开销? 如果我需要从两个数据库中遍历一些相关数据,哪种方法更好(参见伪代码)?
for data in array { redis_select(0) k = redis_get(...) redis_select(1) k2 = redis_get(k) }
要么
redis_select(0) k = [] for data in array { k[x] = redis_get(...) } redis_select(1) k2 = [] for data in array { k2[x] = redis_get(k[x]) }
I am using redis with multiple databases (which I switch by the SELECT command).
I am storing different types of information into redis and I needed to seperate it somehow. I didn't like to prefix the keys to distniguish the information type so I created more databases.
I would like to ask if it was a right decission, with concern for performance ?
Also how much overhead does SELECT cause ? If I need to traverse some related data from say two databases, which aproach is better (see pseudo code) ?
for data in array { redis_select(0) k = redis_get(...) redis_select(1) k2 = redis_get(k) }
or
redis_select(0) k = [] for data in array { k[x] = redis_get(...) } redis_select(1) k2 = [] for data in array { k2[x] = redis_get(k[x]) }
原文:https://stackoverflow.com/questions/8805004
最满意答案
你需要括号:
(4).__str__()
问题是词法学家认为“4.” 将是一个浮点数。
此外,这样做:
x = 4 x.__str__()
So you think you can
dancefloating-point?
123
is just as much of an object as3.14
, the "problem" lies within the grammar rules of the language; the parser thinks we are about to define a float — not an int with a trailing method call.We will get the expected behavior if we wrap the number in parenthesis, as in the below.
>>> (123).__str__() '123'
Or if we simply add some whitespace after
123
:>>> 123 .__str__() '123'
The reason it does not work for
123.__str__()
is that the dot following the123
is interpreted as the decimal-point of some partially declared floating-point.>>> 123.__str__() File "", line 1 123.__str__() ^ SyntaxError: invalid syntax
The parser tries to interpret
__str__()
as a sequence of digits, but obviously fails — and we get a SyntaxError basically saying that the parser stumbled upon something that it did not expect.
Elaboration
When looking at
123.__str__()
the python parser could use either 3 characters and interpret these 3 characters as an integer, or it could use 4 characters and interpret these as the start of a floating-point.123.__str__() ^^^ - int
123.__str__() ^^^^- start of floating-point
Just as a little child would like as much cake as possible on their plate, the parser is greedy and would like to swallow as much as it can all at once — even if this isn't always the best of ideas —as such the latter ("better") alternative is chosen.
When it later realizes that
__str__()
can in no way be interpreted as the decimals of a floating-point it is already too late; SyntaxError.Note
123 .__str__() # works fine
In the above snippet,
123
(note the space) must be interpreted as an integer since no number can contain spaces. This means that it is semantically equivalent to(123).__str__()
.Note
123..__str__() # works fine
The above also works because a number can contain at most one decimal-point, meaning that it is equivalent to
(123.).__str__()
.
For the language-lawyers
This section contains the lexical definition of the relevant literals.
Lexical analysis - 2.4.5 Floating point literals
floatnumber ::= pointfloat | exponentfloat pointfloat ::= [intpart] fraction | intpart "." exponentfloat ::= (intpart | pointfloat) exponent intpart ::= digit+ fraction ::= "." digit+ exponent ::= ("e" | "E") ["+" | "-"] digit+
Lexical analysis - 2.4.4 Integer literals
integer ::= decimalinteger | octinteger | hexinteger | bininteger decimalinteger ::= nonzerodigit digit* | "0"+ nonzerodigit ::= "1"..."9" digit ::= "0"..."9" octinteger ::= "0" ("o" | "O") octdigit+ hexinteger ::= "0" ("x" | "X") hexdigit+ bininteger ::= "0" ("b" | "B") bindigit+ octdigit ::= "0"..."7" hexdigit ::= digit | "a"..."f" | "A"..."F" bindigit ::= "0" | "1"
相关问答
更多-
基本问题:您需要指定数组的类型。 这是使用Gson中的TypeToken完成的。 我希望这有帮助: val listType = object : TypeToken
>() {}.type val json = """["1"]""" val yourClassList :Array = Gson().fromJson(json, listType) print(yourClassList) 请注意,对于基元,它更简单: Gson().fromJson(jso ... -
标量类型的复合文字(Compound literals for scalar types)[2022-01-12]
根据定义,复合文字不包含& address-of运算符。 来自N1570 6.5.2.5/p3 复合文字 : 后缀表达式由带括号的类型名称后跟括号括起的初始值设定项列表组成,是一个复合文字 。 现在,他们的声明: 标量类型和联合类型的复合文字也是允许的,但是复合文字等同于强制转换。 如果我正确阅读它是错误的。 复合文字和强制算子之间的根本区别在于前者是左值 ,如N1570 6.5.2.5/p4 (强调我的): 否则(当类型名称指定对象类型时),复合文字的类型是由类型名称指定的类型。 在任何一种情况下, 结果 ... -
你需要括号: (4).__str__() 问题是词法学家认为“4.” 将是一个浮点数。 此外,这样做: x = 4 x.__str__() So you think you can dance floating-point? 123 is just as much of an object as 3.14, the "problem" lies within the grammar rules of the language; the parser thinks we are about to defi ...
-
Python,将类变量设置为int值不是int类型[duplicate](Python, set class variable to int value not int type [duplicate])[2022-09-09]
如此处所述。 以下方法仅适用于包含方法的类的实例(所谓的描述符类)出现在所有者类中时(描述符必须位于所有者的类字典或父类之一的类字典中)。 As stated here. The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in e ... -
使函数适用于所有数字类型(int,float,long)(make function work with all numeric types (int, float, long))[2022-08-11]
使用内联 : let inline sum a b = a + b 更新: 如果你有兴趣编写你自己的多态数值函数,你应该使用inline和LanguagePrimitives模块。 这里是从线程转换Haskell多态余弦函数到F#的多态余弦函数 : let inline cosine n (x: ^a) = let one: ^a = LanguagePrimitives.GenericOne Seq.initInfinite(fun i -> LanguagePrimitives.Di ... -
答案是肯定的。 知道这一点足够聪明。 这是一个简单程序的拆卸IL( ILSpy提供 ),它具有这样一个任务。 正如您从ldc.r4指令中看到的那样 ,不会发生转换: .method private hidebysig static void Main ( string[] args ) cil managed { // Method begins at RVA 0x2050 // Code size 8 (0x8) .maxstack 1 ...
-
十进制整数文字是第一种类型,可以用int , long或long long 。 50是int类型。 可以使用u或U后缀指定无符号文字。 后缀为u或U十进制整数字是第一种类型,可以用unsigned int , unsigned long或unsigned long long 。 50U的类型为unsigned int 。 A decimal integer literal is of the first type where it can be represented in int, long or lon ...
-
您应该始终明确指出您打算使用的文字类型。 例如,当这种代码时,这将防止出现问题: float foo = 9.0f; float bar = foo / 2; 更改以下内容,截断结果: int foo = 9; float bar = foo / 2; 当您涉及重载和模板时,这也是函数参数的一个问题。 我知道gcc有-Wconversion但我不记得它涵盖的一切。 对于适合int整数值,我通常不会对那些long或unsigned值进行限定,因为通常存在更少的微小错误。 You should always ...
-
VC ++中的文字类型(Literals types in VC++)[2023-09-16]
看来,就你的第一个结果而言,VC ++仍然遵循C89 / 90的规则,其中说(第6.1.3.2节): 整数常量的类型是相应列表中可以表示其值的第一个。 unsuffixed decimal: int , long int , unsigned long int ; [...] 因此,由于4294967295可以表示为unsigned long int ,因此它正在使用它。 在C ++ 98/03中,仍然允许这样做,但不再需要 - 您使用的值大于可以在long int表示的值,这会给出未定义的行为(第2.13 ... -
整数文字(Integer literals)[2021-12-06]
如果不进行强制转换,则无法将具有较高容量的类型的值分配给较低容量的类型。 long可以存储更大的数字,因此无法分配给int 。 You cannot assign a value of a type with higher capacity to a type of lower capacity without casting. long can store larger numbers, so it can't be assigned to int.