这篇文章不是一个真正的问题,但分享一些编码技巧可能会有用.
以下是我想与您分享的内容.我将展示4个做同样事情的例子.但只有最后一个才是最好的.
$foo = 'John SMITH';
echo "Hello $foo, welcome on my website.";
echo "Hello " . $foo . " welcome on my website.";
echo 'Hello ' . $foo . ' welcome on my website.';
echo 'Hello ', $foo , ' welcome on my website.';
Run Code Online (Sandbox Code Playgroud)
我相信大家都知道这echo '$foo'不起作用,但我仍然非常确定你使用双引号来显示一个简单的信息.
这很糟糕.
好吧,让我们开始:第一个是坏的(以及第二个)因为使用双引号强制php扫描字符串以寻找要完成的替换(我的意思是变量).
第二个是好一点,因为php无法替代.
第三个,因为简单的引用更好,所以语言知道他可以只发送文本而不处理,但"坏"的事情是使用连接(点操作符,如第二个例子).
最后一个使用简单的引用和昏迷操作符.为什么这个解决方案更好?
那么,使用第三种解决方案会发生什么?
php创建一个字符串,包含"Hello ",然后它必须放大它,把foo变量的内容("John SMITH"),然后再放大它来放" Welcome on my website."句子.然后,echo可以使用这个,以...回声:)
而在第4个中,唯一要做的echo就是发送"Hello",然后发送$ foo的内容,然后发送"Welcome on my website".到输出,这就是全部!因为echo只需发送文本,而不创建必须放大以包含整个文本的字符串(这将是凹的,必须增长(因为连接)然后显示.
我会尝试找回一些基准并将它们放在这里.
免费评论或反应,并原谅我的英语,这不是我的母语.
我有一个简单的问题:数组变量究竟是如何$_SESSION和$_SERVER请求的?它们是在请求页面时简单设置的,并且在请求页面之前保持不变(例如,
for($i=0, $i<100, ++$i) {
echo $_SERVER['REQUEST_TIME'],'<br/>',$_SESSION['lastActive'],'<hr/>';
}
Run Code Online (Sandbox Code Playgroud)
只会对每个变量做一个请求并发布100行)或者我应该简单地将它们绑定到变量,以避免不必要的服务器请求,例如
$time=$_SERVER['REQUEST_TIME'];
$lastActive=$_SESSION['lastActive'];
for ($i=0, $i<100, ++$i) {
echo $time,'<br/>',$lastActive,'<hr/>';
}
Run Code Online (Sandbox Code Playgroud)
如果这听起来像常识,我很抱歉,但我只是不喜欢设置可以避免的额外变量,从而尝试尽可能多地进行研究.
我正在寻找某种具有固定大小的地图,例如20个条目,但不仅如此,我想只保留最低值,让我说我正在评估某种功能并在我的地图中插入结果(我需要地图,因为我必须保持键值)但我想只有20个最低的结果.我正在考虑排序然后删除最后一个元素,但我需要为数百万条记录做,所以每次添加值时排序效率不高,也许有更好的方法?感谢帮助.
哪个代码更好或优化或有效?
double a;
double b;
if (a == b)
return true;
Run Code Online (Sandbox Code Playgroud)
要么
if (a - b == 0)
return true;
Run Code Online (Sandbox Code Playgroud) 我有一个任务,我必须采取一个程序,并使其在时间上更有效.原始代码是:
#include <stdio.h>
#include <stdlib.h>
// You are only allowed to make changes to this code as specified by the comments in it.
// The code you submit must have these two values.
#define N_TIMES 600000
#define ARRAY_SIZE 10000
int main(void)
{
double *array = calloc(ARRAY_SIZE, sizeof(double));
double sum = 0;
int i;
// You can add variables between this comment ...
long int help;
// ... and this one.
// Please change 'your name' to your actual name.
printf("CS201 …Run Code Online (Sandbox Code Playgroud) 我说没有strlen用于效率目的.因为如果你使用strlen那么你已经迭代了一个字符串,最好的算法总是迭代一个给定的容器不超过一次.所以帮助我思考如何实现一个功能
bool contains ( char * s1, char * s2 )
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
尝试:
bool contains ( char * s1, char * s2 )
{
// returns true or false depending on whether s1 is contained in s2
// define that every string contains the empty string
if ( !*s1 ) return true;
// search for substrings of s2 that equal s1
bool flag = true;
while ( *s2 )
{
char * c …Run Code Online (Sandbox Code Playgroud) 这是需要优化的代码
#include <iostream>
using namespace std;
int main() {
// size of cross, use odd number
int size = 5;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i==j || i+j==size-1) {
cout << "*";
} else {
cout << " ";
}
}
cout << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
* *
* *
*
* *
* *
Run Code Online (Sandbox Code Playgroud)
请让我知道任何来源或参考文献(如果有)...
请查看以下代码
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Employee> employeeCollection = new List<Employee>();
for (int i = 1; i < 10; i++) employeeCollection.Add(new Employee {
EmployeeID = i,
EmployeeName = string.Concat("Employee", i),
Email = string.Concat("Email", i) });
//loop thru every record
foreach (Employee e in employeeCollection)
{
SendEMail(e);
}
}
private static void SendEMail(Employee employee)
{
// do something
}
}
class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName …Run Code Online (Sandbox Code Playgroud) quandry是 - 以下两种方法中的哪一种执行最佳
目标 - 获得类型为Wrapper(下面定义)
标准的对象- 速度超过存储
号.记录 - 约1000-约2000,最大约6K
选择 - 动态创建对象或从字典
执行查找执行速度 - 每秒调用x次
注意 - 我需要首先提供工作代码,然后进行优化,因此如果任何理论家可以提供场景信息背后的一瞥,那么在我进行实际性能测试之前可能会有所帮助.
定义 -
class Wrapper
{
public readonly DataRow Row;
public Wrapper(DataRow dr)
{
Row = dr;
}
public string ID { get { return Row["id"].ToString(); } }
public string ID2 { get { return Row["id2"].ToString(); } }
public string ID3 { get { return Row["id3"].ToString(); } }
public double Dbl1 { get { return (double)Row["dbl1"]; } }
// ... total …Run Code Online (Sandbox Code Playgroud) 我知道这可能与编程中的许多一般规则背道而驰,但你会怎么做呢?我问这个有两个原因:
optimization ×10
java ×3
performance ×3
c ×2
c# ×2
php ×2
.net ×1
algorithm ×1
arrays ×1
c++ ×1
c++14 ×1
collections ×1
dictionary ×1
for-loop ×1
foreach ×1
logic ×1
ram ×1
read-write ×1
session ×1