如果您对为什么__builtins__既是模块又是字典感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于为什么__builtins__既是模块又是字典的详细内容,并且为您提供关
如果您对为什么__builtins__既是模块又是字典感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于为什么__builtins__既是模块又是字典的详细内容,并且为您提供关于c – 为什么__builtin_prefetch在这里没有任何影响?、C++ 中的 __builtin_expect、C++ 的位运算:__builtin, bitset、classActionChains(__builtin__.object)的有价值信息。
本文目录一览:- 为什么__builtins__既是模块又是字典
- c – 为什么__builtin_prefetch在这里没有任何影响?
- C++ 中的 __builtin_expect
- C++ 的位运算:__builtin, bitset
- classActionChains(__builtin__.object)
为什么__builtins__既是模块又是字典
我正在使用内置模块插入一些实例,因此可以出于调试目的对其进行全局访问。__builtins__
模块的问题在于它是主脚本中的模块,也是模块中的命令,但是由于我的脚本(视情况而定)可以是主脚本或模块,因此我必须这样做:
if isinstance(__builtins__, dict): __builtins__[''g_frame''] = ''xxx''else: setattr(__builtins__, ''g_frame'', ''xxx'')
有没有比这更短的解决方法?更重要的是,为什么会__builtins__
这样?
这是一个可以看到这一点的脚本。创建一个模块a.py:
#module-aimport bprint ''a-builtin:'',type(__builtins__)
创建一个模块b.py:
#module-bprint ''b-builtin:'',type(__builtins__)
现在运行python a.py:
$ python a.py b-builtin: <type ''dict''>a-builtin: <type ''module''>
答案1
小编典典我认为您需要该__builtin__
模块(注意单数)。
参见文档:
27.3。
__builtin__
—内置对象CPython实现细节:
大多数模块的名称__builtins__
(请注意''s''
)作为其全局变量的一部分可用。的值__builtins__
通常是此模块或此模块的[sic]__dict__
属性的值。由于这是一个实现细节,因此Python的其他实现可能不会使用它。
c – 为什么__builtin_prefetch在这里没有任何影响?
char c; char * ptr; for( size_t i = 0; i < size ; ++i ) { ptr = ( static_cast<char*>(sentenceMap) + i ); c = *ptr; __builtin_prefetch( ptr + i + 1 ); // some treatment on ptr and c }
如您所见,我添加了一条builtin_prefetch指令,希望在我的循环的下一次迭代中放入缓存.我尝试了不同的值:ptr i 1,ptr i 2,ptr i 10但似乎没有任何改变.
为了测量性能,我使用valgrind的工具cachegrind,它给出了缓存未命中数的指示.在行c = * ptr上,当未设置__builtin_prefetch时,cachegrind记录632,378 DLmr(L3缓存未命中).但奇怪的是,无论我设置为__builtin_prefetch的参数如何,此值都不会更改.
对此有何解释?
解决方法
总结
以上是小编为你收集整理的c – 为什么__builtin_prefetch在这里没有任何影响?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
C++ 中的 __builtin_expect
这个指令是 gcc 引入的,作用是允许程序员将最有可能执行的分支告诉编译器。这个指令的写法为:
__builtin_expect(EXP, N)
。
意思是:EXP==N 的概率很大。
一般的使用方法是将__builtin_expect
指令封装为 likely
和 unlikely
宏。这两个宏的写法如下.
#define likely(x) __builtin_expect(!!(x), 1) //x很可能为真
#define unlikely(x) __builtin_expect(!!(x), 0) //x很可能为假
内核中的 likely () 与 unlikely ()
首先要明确:
if(likely(value)) //等价于 if(value)
if(unlikely(value)) //也等价于 if(value)
__builtin_expect()
是 GCC (version>= 2.96)提供给程序员使用的,目的是将 “分支转移” 的信息提供给编译器,这样编译器可以对代码进行优化,以减少指令跳转带来的性能下降。
__builtin_expect((x),1)
表示 x 的值为真的可能性更大;
__builtin_expect((x),0)
表示 x 的值为假的可能性更大。
也就是说,使用 likely()
,执行 if 后面的语句的机会更大,使用 unlikely()
,执行 else 后面的语句的机会更大。通过这种方式,编译器在编译过程中,会将可能性更大的代码紧跟着起面的代码,从而减少指令跳转带来的性能上的下降。
例子
int x, y;
if(unlikely(x > 0))
y = 1;
else
y = -1;
上面的代码中 gcc 编译的指令会预先读取 y = -1 这条指令,这适合 x 的值大于 0 的概率比较小的情况。如果 x 的值在大部分情况下是大于 0 的,就应该用 likely (x> 0),这样编译出的指令是预先读取 y = 1 这条指令了。这样系统在运行时就会减少重新取指了。
C++ 的位运算:__builtin, bitset
int __builtin_ffs (unsigned int x)
返回二进制表示中x
的最后一位 $1$(最右边的)是从后向前第几位,比如 $7368(1110011001000)$ 返回 $4$ 。int __builtin_clz (unsigned int x)
返回二进制表示中前导 $0$ 的个数。int __builtin_ctz (unsigned int x)
返回二进制表示中末尾 $0$ 的个数。int __builtin_popcount (unsigned int x)
返回二进制表示中 $1$ 的个数。int __builtin_parity (unsigned int x)
返回x
的奇偶校验位,也就是x
的 $1$ 的个数模 $2$ 的结果。
这些函数都有相应的 unsigned long
和 unsigned long long
版本,只需在函数名后面加上 l
或 ll
即可,如 int __builtin_clzll
bitset 定义在 <bitset>
库中。
bitset<8> b1; // [0,0,0,0,0,0,0,0]
bitset<8> b2(42); // [0,0,1,0,1,0,1,0]
bitset<17> bs(0xfff0); // [1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0]
string bit_string = "110010";
bitset<8> b3(bit_string); // [0,0,1,1,0,0,1,0]
bitset<8> b4("110010"); // [0,0,1,1,0,0,1,0]
cout << b4[0] << b4[1] << b4[2] << b4[3] << b4[4] << endl; // 0,1,0,0,1
cout << b3 << endl; // 00110010 (1. 不是 50! 2. 会输出前导 0! )
b4[0] = true; // b4 = [0,0,1,1,0,0,1,1]
b4.set(3); // [0,0,1,1,1,0,1,1]
b4.set(5); // [0,0,1,1,1,0,1,1] , 并没有变化
b4.unset(4); // [0,0,1,0,1,0,1,1]
b4.flip(1); // [0,0,1,0,1,0,0,1]
b4.flip(1); // [0,0,1,0,1,0,1,1]
b4.flip(2); // [0,0,1,0,1,1,1,1]
b4.flip(0); // [0,0,1,0,1,1,1,0]
cout << b2.size() << '','' << bs.size << endl;// 8,17
cout << b2.count() << endl;// 3
cout << bs.count() << endl;// 13
string
另外,bitset 支持类似一个整数的操作,可以比较相等还是不等(但不能比较谁大谁小),可以左移右移,可以按位取与或非异或。但是一个 bitset 不能和一个真正的整数(如 int 型整数)进行这些操作。
classActionChains(__builtin__.object)
ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. ActionChains是低层次自动化交互方式,例如鼠标移动,鼠标按钮操作,按键和右键菜单交互 This is useful for doing more complex actions like hover over and drag and drop. 对于执行复杂操作,像悬停,拖拽,拖放等是有用的。
Generate user actions.产生用户操作 When you call methods for actions on the ActionChains object,the actions are stored in a queue in the ActionChains object.When you call perform(), the events are fired in the order they are queued up. 当你调用ActionChains对象的操作方法时,操作按顺序存储在ActionChains队列中。当调用perform(),事件按顺序被触发。
ActionChains can be used in a chain pattern:: ActionChains被应用于chain模式
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform
()
Or actions can be queued up one by one, then performed.:: 或者操作按序排列,然后执行:
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
Either way, the actions are performed in the order they are called, one after another. 不管怎样,这些操作按他们调用顺序一个接一个的执行
Methods defined here:
enter(self) Context manager so ActionChains can be used in a ''with .. as'' statements.
exit(self, _type, _value, _traceback)
init(self, driver) Creates a new ActionChains.添加一个新ActionsChains
:Args:
- driver: The WebDriver instance which performs user actions.执行用户操作的 WebDriver 实例
click(self, on_element=None) Clicks an element.点击一个元素
:Args:
- on_element: The element to click. If None, clicks on current mouse position.被点击的元素,如果为空,点击当前鼠标的位置。
click_and_hold(self, on_element=None) Holds down the left mouse button on an element.在一个元素上点击鼠标左键
:Args:
- on_element: The element to mouse down. If None, clicks on current mouse position.被点击的元素,如果为空,点击当前鼠标的位置。
context_click(self, on_element=None) Performs a context-click (right click) on an element.对一个元素执行点击右键操作
:Args:
- on_element: The element to context-click. If None, clicks on current mouse position.被点击的元素,如果为空,点击当前鼠标的位置。
double_click(self, on_element=None) Double-clicks an element.双击一个元素
:Args:
- on_element: The element to double-click. If None, clicks on current mouse position.被双击的元素,如果为空,点击当前鼠标的位置
drag_and_drop(self, source, target) Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.在源元素上点击鼠标左键,然后移动至目标元素并松开鼠标按钮。
:Args:
- source: The element to mouse down.被按下鼠标的元素
- target: The element to mouse up.被松开鼠标的元素
drag_and_drop_by_offset(self, source, xoffset, yoffset) Holds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.在源元素上点击鼠标左键,然后移动至目标位置并松开鼠标按钮。
:Args:
- source: The element to mouse down.被按下鼠标的元素
- xoffset: X offset to move to.水平移动偏移量
- yoffset: Y offset to move to.垂直移动偏移量
key_down(self, value, element=None) Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt and Shift).按下键盘上的某个键
:Args:
- value: The modifier key to send. Values are defined in
Keys
class.发送key,值在Keys类总定义。 - element: The element to send keys. If None, sends a key to current focused element.发送按键元素,如果为空,发送一个键到当前的焦点元素。
Example, pressing ctrl+c::
ActionChains(driver).key_down(Keys.CONTROL).send_keys(''c'').key_up(Keys.CONTROL).perform()
key_up(self, value, element=None) Releases a modifier key.松开按键
:Args:
- value: The modifier key to send. Values are defined in Keys class.发送key,值在Keys类总定义。
- element: The element to send keys. If None, sends a key to current focused element.发送按键元素,如果为空,发送一个键到当前的焦点元素。
Example, pressing ctrl+c::
ActionChains(driver).key_down(Keys.CONTROL).send_keys(''c'').key_up(Keys.CONTROL).perform()
move_by_offset(self, xoffset, yoffset) Moving the mouse to an offset from current mouse position.将鼠标从当前位置移动值另一个位置。
:Args:
- xoffset: X offset to move to, as a positive or negative integer.水平移动偏移量,x可以使正整数或负整数。
- yoffset: Y offset to move to, as a positive or negative integer.垂直移动偏移量,y可以使正整数或负整数。
move_to_element(self, to_element) Moving the mouse to the middle of an element.移动鼠标到元素中间位置
:Args:
- to_element: The WebElement to move to.被移动的元素。
move_to_element_with_offset(self, to_element, xoffset, yoffset) Move the mouse by an offset of the specified element.Offsets are relative to the top-left corner of the element.按指定元素的偏移量移动鼠标。偏移量是相对于元素左上角移动
:Args:
- to_element: The WebElement to move to.被移动的元素
- xoffset: X offset to move to.水平移动偏移量
- yoffset: Y offset to move to.垂直移动偏移量
perform(self) Performs all stored actions.执行所有存储的操作
release(self, on_element=None) Releasing a held mouse button on an element.松开元素上的鼠标按钮
:Args:
- on_element: The element to mouse up. If None, releases on current mouse position.被松开的鼠标按钮,若为空,则为鼠标当前位置
reset_actions(self) Clears actions that are already stored on the remote end.清除已经存储在远程终端上的操作。
*send_keys(self, keys_to_send) Sends keys to current focused element.发送按键到当前所在元素
:Args:
- keys_to_send: The keys to send. Modifier keys constants can be found in the ''Keys'' class.发送按键,keys
*send_keys_to_element(self, element, keys_to_send) Sends keys to an element.发送按键到一个元素。
:Args:
- element: The element to send keys.被发送按键的元素。
- keys_to_send: The keys to send. Modifier keys constants can be found in the ''Keys'' class.被发送的keys,keys存在于Kyes类中
Data descriptors defined here:
dict dictionary for instance variables (if defined)
weakref list of weak references to the object (if defined)
关于为什么__builtins__既是模块又是字典的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 为什么__builtin_prefetch在这里没有任何影响?、C++ 中的 __builtin_expect、C++ 的位运算:__builtin, bitset、classActionChains(__builtin__.object)等相关知识的信息别忘了在本站进行查找喔。
本文标签: