在C/ c++中,全局变量像我的教授认为的那样糟糕吗?
在C/ c++中,全局变量像我的教授认为的那样糟糕吗?
我用unsigned user_id创建了一个迁移。我如何编辑user_id在一个新的迁移,也使它为空()?
Schema::create('throttle', function(Blueprint $table)
{
$table->increments('id');
// this needs to also be nullable, how should the next migration be?
$table->integer('user_id')->unsigned();
}
如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。
我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
I noticed that when I place a white or black UIImage into a UISegmentedControl it automatically color masks it to match the tint of the segmented control. I thought this was really cool, and was wondering if I could do this elsewhere as well. For example, I have a bunch of buttons that have a uniform shape but varied colors. Instead of making a PNG for each button, could I somehow use this color masking to use the same image for all of them but then set a tint color or something to change their actual color?
想到解释什么是offsetHeight, clientHeight和scrollHeight或offsetWidth, clientWidth和scrollWidth之间的区别?
在客户端工作之前,必须了解这个区别。否则他们的一半生命将花在修复UI上。
小提琴,或内联如下:
function whatis(propType) { var mainDiv = document.getElementById("MainDIV"); if (window.sampleDiv == null) { var div = document.createElement("div"); window.sampleDiv = div; } div = window.sampleDiv; var propTypeWidth = propType.toLowerCase() + "Width"; var propTypeHeight = propType + "Height"; var computedStyle = window.getComputedStyle(mainDiv, null); var borderLeftWidth = computedStyle.getPropertyValue("border-left-width"); var borderTopWidth = computedStyle.getPropertyValue("border-top-width"); div.style.position = "absolute"; div.style.left = mainDiv.offsetLeft + Math.round(parseFloat((propType == "client") ? borderLeftWidth : 0)) + "px"; div.style.top = mainDiv.offsetTop + Math.round(parseFloat((propType == "client") ? borderTopWidth : 0)) + "px"; div.style.height = mainDiv[propTypeHeight] + "px"; div.style.lineHeight = mainDiv[propTypeHeight] + "px"; div.style.width = mainDiv[propTypeWidth] + "px"; div.style.textAlign = "center"; div.innerHTML = propTypeWidth + " X " + propTypeHeight + "( " + mainDiv[propTypeWidth] + " x " + mainDiv[propTypeHeight] + " )"; div.style.background = "rgba(0,0,255,0.5)"; document.body.appendChild(div); } document.getElementById("offset").onclick = function() { whatis('offset'); } document.getElementById("client").onclick = function() { whatis('client'); } document.getElementById("scroll").onclick = function() { whatis('scroll'); } #MainDIV { border: 5px solid red; } <button id="offset">offsetHeight & offsetWidth</button> <button id="client">clientHeight & clientWidth</button> <button id="scroll">scrollHeight & scrollWidth</button> <div id="MainDIV" style="margin:auto; height:200px; width:400px; overflow:auto;"> <div style="height:400px; width:500px; overflow:hidden;"> </div> </div>
根据我的理解,Python有一个单独的函数名称空间,所以如果我想在函数中使用全局变量,我可能应该使用global。
然而,我能够访问一个全局变量,即使没有全局:
>>> sub = ['0', '0', '0', '0']
>>> def getJoin():
... return '.'.join(sub)
...
>>> getJoin()
'0.0.0.0'
为什么会这样?
另请参阅第一次使用后重新分配局部变量时发生的UnboundLocalError,以了解试图分配给全局变量而不使用全局变量时发生的错误。有关如何使用全局变量的一般问题,请参阅在函数中使用全局变量。
我只是在修改c#深度的第4章,它涉及到可空类型,我增加了一个关于使用“as”操作符的章节,它允许你这样写:
object o = ...;
int? x = o as int?;
if (x.HasValue)
{
... // Use x.Value in here
}
我认为这真的很整洁,它可以比c# 1的等效方法提高性能,使用“is”后面加强制转换——毕竟,这样我们只需要要求进行一次动态类型检查,然后进行简单的值检查。
This appears not to be the case, however. I've included a sample test app below, which basically sums all the integers within an object array - but the array contains a lot of null references and string references as well as boxed integers. The benchmark measures the code you'd have to use in C# 1, the code using the "as" operator, and just for kicks a LINQ solution. To my astonishment, the C# 1 code is 20 times faster in this case - and even the LINQ code (which I'd have expected to be slower, given the iterators involved) beats the "as" code.
对于可空类型的isinst的。net实现真的很慢吗?是额外的开箱。是什么导致了这个问题?还有其他解释吗?目前,我觉得我必须在对性能敏感的情况下使用此功能时提出警告……
结果:
演员:10000000:121 As: 10000000: 2211 Linq: 10000000: 2143
代码:
using System;
using System.Diagnostics;
using System.Linq;
class Test
{
const int Size = 30000000;
static void Main()
{
object[] values = new object[Size];
for (int i = 0; i < Size - 2; i += 3)
{
values[i] = null;
values[i+1] = "";
values[i+2] = 1;
}
FindSumWithCast(values);
FindSumWithAs(values);
FindSumWithLinq(values);
}
static void FindSumWithCast(object[] values)
{
Stopwatch sw = Stopwatch.StartNew();
int sum = 0;
foreach (object o in values)
{
if (o is int)
{
int x = (int) o;
sum += x;
}
}
sw.Stop();
Console.WriteLine("Cast: {0} : {1}", sum,
(long) sw.ElapsedMilliseconds);
}
static void FindSumWithAs(object[] values)
{
Stopwatch sw = Stopwatch.StartNew();
int sum = 0;
foreach (object o in values)
{
int? x = o as int?;
if (x.HasValue)
{
sum += x.Value;
}
}
sw.Stop();
Console.WriteLine("As: {0} : {1}", sum,
(long) sw.ElapsedMilliseconds);
}
static void FindSumWithLinq(object[] values)
{
Stopwatch sw = Stopwatch.StartNew();
int sum = values.OfType<int>().Sum();
sw.Stop();
Console.WriteLine("LINQ: {0} : {1}", sum,
(long) sw.ElapsedMilliseconds);
}
}
我想在c#中解析一个字符串为可空int。ie。我想返回字符串的int值或null,如果它不能被解析。
我有点希望这能起作用
int? val = stringVal as int?;
但这行不通,我现在要做的是写这个扩展方法
public static int? ParseNullableInt(this string value)
{
if (value == null || value.Trim() == string.Empty)
{
return null;
}
else
{
try
{
return int.Parse(value);
}
catch
{
return null;
}
}
}
有更好的办法吗?
编辑:感谢TryParse的建议,我确实知道这一点,但它的工作原理是一样的。我更感兴趣的是知道是否有一个内置的框架方法,将直接解析成一个可空的int?
我已经为一个UIButton添加了多个target-action-forControlEvents:。我想去除所有这些在一个去不脱销任何东西。然后我会设定新的目标。
这可能吗?我该怎么做?