variable-assignment

What's more expensive, comparison or assignment?

纵然是瞬间 提交于 2019-12-22 03:21:54
问题 I've started reading Algorithms and I keep wondering, when dealing with primitives of the same type, which is the more expensive operation, assignment or comparison? Does this vary a great deal between languages? 回答1: Micro-optimization is almost always the wrong thing to do. Don't even start on it unless the program runs too slowly, and you use a profiler to determine exactly where the slow parts are. Once you've done that, my advice is to see about improving code and data locality, because

CodeIgniter, pass data from model to controller to use in view

≡放荡痞女 提交于 2019-12-21 21:39:27
问题 I want to pass data queried in my model to the controller, to do so I am using return $data. Then in the controller I use $this->load->view('my_view', $data); From my understanding var_dump($data); in the view should show me the results from the query... This is not the case. I am getting "undefined variable data" and NULL from the var_dump($data); . Here is my model: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Manage_accounts_model extends CI_Model {

What is this assignment construct called? And can you do it in Php?

醉酒当歌 提交于 2019-12-21 19:59:21
问题 I have often used the following construct in Javascript: var foo = other_var || "default_value"; In Javascript, if the left side is falsy, then the value on the right side is assigned. It is very handy, and saves writing longer and unnecessarily explicit ternary expressions. Is there a name for this sort of construct ? Bonus: Is there a trick to do this in Php without using a ternary operator? PS: another variant is to throw an error if you don't get a truthy value, instead of giving a

Makefile Variable Assignment Executes Early

微笑、不失礼 提交于 2019-12-21 12:32:15
问题 I have a Makefile rule that requires storing the results from shell commands into variables for later use. For some reason, it seems that the $(shell) call executes as soon as my rule is match, as opposed to when it is encountered during execution. The file is as follows: TMPDEV=/tmp/disk.img $(TMPDEV): fallocate -l 806354944 $(TMPDEV) sudo parted --script $(TMPDEV) unit s mklabel msdos \ mkpart primary fat16 2048 526335 \ mkpart primary fat32 526336 1050623 \ mkpart primary NTFS 1050624

Why does an assignment for double-sliced numpy arrays not work?

只谈情不闲聊 提交于 2019-12-21 07:58:29
问题 why do the following lines not work as I expect? import numpy as np a = np.array([0,1,2,1,1]) a[a==1][1:] = 3 print a >>> [0 1 2 1 1] # I would expect [0 1 2 3 3] Is this a 'bug' or is there another recommended way to this? On the other hand, the following works: a[a==1] = 3 print a >>> [0 3 2 3 3] Cheers, Philipp 回答1: It appears you simply can't do an assignment through a double-slice like that. This works though: a[numpy.where(a==1)[0][1:]] = 3 回答2: It's related to how fancy indexing works.

Tensorflow: When are variable assignments done in sess.run with a list?

血红的双手。 提交于 2019-12-21 03:58:27
问题 I have thought that variable assignments are done after all operations in a list given to sess.run, but the following code returns different results at different execution. It seems randomly run operations in the list and assign the variable after the run of the operation in the list. a = tf.Variable(0) b = tf.Variable(1) c = tf.Variable(1) update_a = tf.assign(a, b + c) update_b = tf.assign(b, c + a) update_c = tf.assign(c, a + b) with tf.Session() as sess: sess.run(initialize_all_variables)

cygwin - cannot execute binary file

馋奶兔 提交于 2019-12-21 01:58:31
问题 I am trying to run these two .data files from my c++ code for my assignment. I have been provided with all the files and are meant to only implement a few functions for the program (everything should be able to compile running the make command). I used to run a MAC and only just started using windows (win 7) because work gave me a free laptop. Anyways...I installed cygwin, added the gcc-c++ compiler, gdb and make packages to my cygwin. But when i run the command ./file ./data it comes up with

Which type trait would indicate that type is memcpy assignable? (tuple, pair)

情到浓时终转凉″ 提交于 2019-12-20 20:31:34
问题 I would like to know what type introspection I can do to detect types that assignable by simply raw memory copy? For example, as far I understand, built-in types tuples of built-in types and tuple of such tuples, would fall in this category. The motivation is that I want to transport raw bytes if possible. T t1(...); // not necessarely default constructible T t2(...); t1 = t2; // should be equivalent to std::memcpy(&t1, &t2, sizeof(T)); // t1 is now an (independent) copy of the value of t2,

Overloading assignment operator for pointers to two different classes

老子叫甜甜 提交于 2019-12-20 07:26:01
问题 My Question: I'm trying to overload the assignment operator for pointers to two different classes. Here is an example: dc.h: #ifndef DC_H_ #define DC_H_ #include "ic.h" class dc { double d; char c; public: dc(): d(0), c(0) { } dc(double d_, char c_): d(d_), c(c_) { } dc* operator=(const ic* rhs); ~dc() { } }; #endif /* DC_H_ */ class ic.h: #ifndef IC_H_ #define IC_H_ class ic { int i; char c; public: ic(): i(0), c(0) { } ic(int i_, char c_): i(i_), c(c_) { } ~ic() { } }; #endif /* IC_H_ */ dc

matlab: filling matrix diagonalwise [duplicate]

夙愿已清 提交于 2019-12-20 06:21:14
问题 This question already has answers here : adding values to diagonals of matrix using element-wise addition in matlab (3 answers) Closed 4 years ago . I have an (2n-1)-by-1 vector with certain values and I want to obtain an n-n matrix with the diagonals filled using the same value. Eg. if I have a = [1; 2; 3; 4; 5]; I want to obtain A = [[3 4 5];[2 3 4];[1 2 3]] = 3 4 5 2 3 4 1 2 3 My matrix dimensions are a lot bigger so I'd want this as efficient as possible. I already found following