我有这个docker-compose.yaml
version: "3.7"\nservices:\n busybox:\n image: busybox:latest\n entrypoint: \'\'\n environment:\n - bar=${foo:-baz}\n command: \n - "/bin/sh" \n - "-c" \n - "echo $$bar"\nRun Code Online (Sandbox Code Playgroud)\n我正在这样运行:
\ndocker-compose up && docker-compose down\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n[+] Running 2/2\n \xe2\xa0\xbf Network interpolation-poblem_default Created\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1 Created\nAttaching to interpolation-poblem-busybox-1\ninterpolation-poblem-busybox-1 | baz\ninterpolation-poblem-busybox-1 exited with code 0\n[+] Running 2/2\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1 Removed\n \xe2\xa0\xbf Network interpolation-poblem_default Removed\nRun Code Online (Sandbox Code Playgroud)\n它工作正常 -foo替换为baz以下内容:/bin/sh -c "echo $bar"
interpolation-poblem-busybox-1 | baz\nRun Code Online (Sandbox Code Playgroud)\n … environment-variables substitution string-interpolation docker docker-compose
我正在优化我们的调试打印设施(类)。该类大致简单,具有全局“启用”布尔值和PrineDebug例程。
我正在研究该方法在“禁用”模式下的性能PrintDebug,尝试创建一个在不需要调试打印的情况下对运行时影响较小的框架。
在探索过程中,我发现了以下结果,这让我感到惊讶,我想知道我在这里错过了什么?
public class Profiler
{
private bool isDebug = false;
public void PrineDebug(string message)
{
if (isDebug)
{
Console.WriteLine(message);
}
}
}
[MemoryDiagnoser]
public class ProfilerBench
{
private Profiler profiler = new Profiler();
private int five = 5;
private int six = 6;
[Benchmark]
public void DebugPrintConcat()
{
profiler.PrineDebug("sometext_" + five + "_" + six);
}
[Benchmark]
public void DebugPrintInterpolated()
{
profiler.PrineDebug($"sometext_{five}_{six}");
}
}
Run Code Online (Sandbox Code Playgroud)
在 BenchmarkDotNet 下运行此基准测试..结果如下:
| Method | Mean | Error | …Run Code Online (Sandbox Code Playgroud) 从 Python 3.8 开始,可以在 f 字符串中使用自记录表达式,如下所示:
>>> variable=5
>>> print(f'{variable=}')
variable=5
Run Code Online (Sandbox Code Playgroud)
C# 中有等效的功能吗?
我正在使用Scala将文件写入磁盘.
要创建将写入文件的整个String,我正在迭代我的数据并将所有信息附加到StringBuilder对象.
例如:
val moreData = getMoreData
strBuilder.append(moreData)
strBuilder.append("even more data")
//...
strBuilder.toString
Run Code Online (Sandbox Code Playgroud)
在操作结束时,我然后调用StringBuilder的toString方法,并写入Path.
我知道Scala对字符串有编译优化,所以我的问题是:
哪种方法具有更好的性能.String-Interpolation-Concatenation或StringBuilder?
这些编译优化是否与StringBuilder有某种关联?换句话说,StringBuilder追加操作是否有优化?
我正在研究一个包含一些VB.NET项目的遗留解决方案.
像这样的代码
Throw New InvalidOperationException($"Cannot update {entity.Id} because it does not yet exist.")
Run Code Online (Sandbox Code Playgroud)
在美元符号处产生红色波浪形错误"意外令牌".我通过在相关的.vbproj文件中为每个构建配置添加14来构建代码,但是我仍然被这些红色的波浪线所困扰.不仅在美元符号下,而且在其后面的每个End If/End Using/End Sub下面,这是非常烦人的.
使用VS2015 Update 1,即使在同一解决方案中,C#6语言功能(包括字符串插值)也始终正常工作.
我创建文件 - >新项目(暗示,一个全新的解决方案) - > VB.NET类库,代码仍然很好,但IDE仍然给我波浪形.也许问题在于一些模糊的machine.config设置?我需要安装一些VS插件吗?
vb.net string resharper string-interpolation visual-studio-2015
所以我将此作为邮件发送脚本的一部分:
try:
content = ("""From: Fromname <fromemail>
To: Toname <toemail>
MIME-Version: 1.0
Content-type: text/html
Subject: test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
""")
Run Code Online (Sandbox Code Playgroud)
...
mail.sendmail('from', 'to', content)
Run Code Online (Sandbox Code Playgroud)
我想每次都使用不同的主题(让我们说它是函数参数).
我知道有几种方法可以做到这一点.
但是,我也使用ProbLog来处理我的一些其他脚本(一种基于Prolog语法的概率编程语言).据我所知,在Python中使用ProbLog的唯一方法是通过字符串,如果字符串在几个部分中断了; example =("""string""",variable,"""string2"""),以及上面的电子邮件示例中,我无法使其工作.
我实际上有一些脚本,在多行字符串中使用变量可能很有用,但你明白了.
有没有办法让这项工作?提前致谢!
我有一个插值字符串,以一个参数结束,后跟一个右括号.它需要有一个格式化参数,但是该字符串将第一个双括号作为转义括号并保留为关闭参数的括号:
> $"foo:{16:x}"
"foo:10"
> $"foo:{16:x}}}"
"foo:x}"
Run Code Online (Sandbox Code Playgroud)
如何正确编写插值字符串,以便我得到foo:10}?
fun main(args: Array<String>) {
var _array = arrayOf(1 , 2 , 3.14 , 'A', "item" , "a b c d", 4)
println("$_array[3]") // [Ljava.lang.Object;@1b6d3586[3]
println("${_array[3]}") // A
println(_array[3]) // A
println( _array[3] + " is _array's item") // ERROR
println( "" + _array[3] + " is _array's item") // A is _array's item
}
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么上面的代码产生不同的输出
乍一看,它似乎只为每个嵌入式表达式节省了2个字符:
Console.WriteLine($"This is time number {i} that I, {name}, printed this statement.");
Console.WriteLine("This is time number "+i+" that I, "+name+", printed this statement.");
Run Code Online (Sandbox Code Playgroud)
这真的值得一整套语言功能吗?
话虽这么说,我不得不承认,我确实更喜欢看花括号,因此我一直在使用字符串插值。但是,为什么我更喜欢它呢?什么心理现象更喜欢{hello}到"+hello+"?似乎有点武断。
字符串插值还有其他好处,可以保证整个语言功能吗?还是真的只是关于可读性?
"+=> {和+"=> 的简单文本替换}?我知道它已被编译为string.Format。因此,生成的二进制文件是不同的,但是执行似乎是相同的,给与或取一些非常小的性能差异。
c# ×4
string ×4
python ×2
.net ×1
c#-6.0 ×1
docker ×1
ecmascript-6 ×1
javascript ×1
kotlin ×1
performance ×1
resharper ×1
scala ×1
substitution ×1
vb.net ×1