首页 \ 问答 \ 在Python中,使用变量的方式在速度,CPU,内存等方面更有效率?(Which way of using variables is more efficient in terms of speed, cpu, memory, etc in python?)

在Python中,使用变量的方式在速度,CPU,内存等方面更有效率?(Which way of using variables is more efficient in terms of speed, cpu, memory, etc in python?)

假设我们有一个函数想要对objA的属性z进行一些操作。 objA是objB的一个属性,objB是objC等的一个属性......这两种方法哪一个更快? 有什么区别吗?

方法1:对函数中的每个语句使用objC.objB.objA.z

方法2:在函数中分配一个像x这样的局部变量:

x=objC.objB.objA.z

然后在x上运行,然后将输出分配给更好的变量。

我知道方法2在编写实际代码方面更容易,但没有定义新的变量会花费更多的内存吗? 哪种方法更加pythonic,除了上述方法之外,还有其他更好的方法吗?


Assume we have a function which wants to operate some actions on attribute z of objA. objA is a property of objB and objB is a property of objC etc... which of these two approaches is faster ? Is there any difference?

Approach 1: Using objC.objB.objA.zfor every statement in the function.

Approach 2: Assigning a local variable like x in function as:

x=objC.objB.objA.z

then operate on x, then assign the output to the preferable variable.

I know Approach 2 makes it easier in terms of writing the actual code but doesn't defining a new variable cost more memory? Which approach is more pythonic and is there any other (better) way to do things other than aforementioned approaches?


原文:https://stackoverflow.com/questions/16029600
更新时间:2023-06-19 08:06

最满意答案

字段element_type设置为父类,因为ActiveRecord期望您在从其他模型派生时使用单表继承。 该字段将引用基类,因为它引用了存储每个实例的表。

如果CachedElement存储在它们自己的表中,那么使用Ruby模块替换继承的使用可能会更有帮助。 在类之间共享逻辑的标准方法是使用混合而不是继承。 例如:

module Cacheable
  # methods that should be available for all cached models
  # ...
end

class Outcome < ActiveRecord::Base
  include Cacheable
  # ... 
end

您现在可以轻松地使用多态关联,并将element_type设置为正确的类。


The field element_type is set to the parent class because ActiveRecord expects you to use single-table inheritance when deriving from other models. The field will reference the base class because it refers to the table that each instance is stored in.

If the children of CachedElement are stored in their own tables, it may be more helpful to replace the use of inheritance with the use of Ruby modules. The standard approach for sharing logic between classes is to use mix-ins instead of inheritance. For example:

module Cacheable
  # methods that should be available for all cached models
  # ...
end

class Outcome < ActiveRecord::Base
  include Cacheable
  # ... 
end

You can now easily use polymorphic associations as you have been doing already, and element_type will be set to the proper class.

相关问答

更多
  • 单表继承! 通过这种方式,您可以让每种类型的请求都具有自定义验证,而每个请求仍然存在于同一个表中。 class CreateUserRequests < ActiveRecord::Migration def self.up create_table :user_requests do |t| t.string :string_data, :type t.integer :user_id, :integer_data t.timestamps end ...
  • 字段element_type设置为父类,因为ActiveRecord期望您在从其他模型派生时使用单表继承。 该字段将引用基类,因为它引用了存储每个实例的表。 如果CachedElement存储在它们自己的表中,那么使用Ruby模块替换继承的使用可能会更有帮助。 在类之间共享逻辑的标准方法是使用混合而不是继承。 例如: module Cacheable # methods that should be available for all cached models # ... end class O ...
  • 由于status描述了Users 之间的关联,而不是Users本身,因此它应该是Friendship模型的字段或与之关联的模型,而不是附加到User模型。 Since status describes the association between Users, not the Users themselves, it should be a field of the Friendship model, or a model associated with it, rather than attached ...
  • 嗨,您可以轻松地包括user_id和department_id。 您没有必要通过控制台完成它。 在迁移文件中,您可以手动将其写入迁移文件,如下所示: add_foreign_key :, :, column: :, name: "_FK", on_delete:cascade 这是 ...
  • 我认为你的前提是错误的,因为你不想做什么。 我解释: 你想要什么,可以通过像这样的帮助方法完成: class Course < AR def title content.title end end 这是丑陋的,违反了曾经写过的每一个编码论点,以及其糟糕的做法。 但是,除了一些可以避免必须逐个编写帮助器方法的元魔法(注意我说写,因为最终它们将被实现),最终的结果必须是这样。 没有其他办法可以做到这一点。 所以我的回答是,不要这样做。 理解多态并不容易。 它容易实现,但很难知道什么时候。 你只 ...
  • 为了使它工作,你必须在数据中嵌入一些对象类型信息(它只对反序列化很有用),并且通常使用外部模式定义,否则就不需要它(如xml的XML Schema;因为它基本上是泛型类型系统) 。 这与ORM有相同的问题:Hibernate必须使用丑陋的解决方法(n + 1路连接,“超级表”或鉴别器字段)。 或者说它的另一种方式:数据映射/绑定与对象序列化/反序列化(后者试图保留更多对象标识)不完全相同。 在这里,我假设你想要的基本上是这样的: Pet pet = mapper.readValue(jsonString, ...
  • 你提到的两个框架都可以处理满足这些要求的网站。 您还应该将Django和Symfony添加到您可能选择的列表中,因为它们也可以满足您的需求。 你有使用Ruby / PHP / Python构建应用程序的经验吗? Both of the frameworks you mention could handle a web site with those requirements. You should also add Django and Symfony to your list of possible ch ...
  • instantrails应该是你正在寻找的。 http://rubyforge.org/frs/?group_id=904 instantrails should be what you're looking for. http://rubyforge.org/frs/?group_id=904
  • 两个主要问题: 根据Rails文档,关系方法的字面意思是不正确的。 polymorphic: true应该在belongs_to部分。 并且as: :messageable应该在has_one或has_many部分。 2.主要问题是建模不正确 信件,电子邮件或手机都是一种消息,它们既不属于也没有消息,它们都是消息。 所以,这种关系不是必要的,事实上他们应该与发送或接收消息的人建立关系。 界面看起来像这样 sender = User.find(123) receiver = User.find(456) me ...
  • 在我们进一步讨论之前,我确实注意到了一个问题 - 您不需要与t.references的has_many端进行t.references 。 所以你在create_user模型中不需要它。 它所做的就是创建phonable_id和phoneable_type列,您只需要在多态模型中使用该列。 您正在使用fields_for方法走向正确的路径。 但为了实现这个目标,你需要告诉模型如何处理这些领域。 您可以使用accepts_nested_attributes_for类方法执行此操作。 class User < A ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。