I need to improve as much as possible the performance of the function defined below. It is called millions of times, and the <html>
<head>
<title></title>
</head>
<body>
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "blog_samples";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$rows_count = count($_POST["name"]);
for($i=0;$i<$rows_count;$i++){
// PREVENTING SQL INJECTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$name = mysqli_real_escape_string($conn,$_POST["name"][$i]);
$code = mysqli_real_escape_string($conn,$_POST["code"][$i]);
$quantity = intval($_POST["quantity"][$i]);
$price = mysqli_real_escape_string($conn,$_POST["price"][$i]);
$sql = "INSERT INTO order_table (name, code, quantity, price)
VALUES ('$name','$code','$quantity','$price')";
$query = mysqli_query($conn,$sql);
}
if(mysqli_affected_rows($conn)>0) {
echo "Record add successfully";
}
mysqli_close($conn);
?>
</body>
</html>
loop is currently the bottleneck of my code.
Connection conn = DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"?useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName, Utils.DBPassword);
I've tried with:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("application/json");
request.setCharacterEncoding("UTF-8");
String content = request.getParameter("content");
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection(Utils.DBURL+"/"+Utils.DBName+"?
useUnicode=true&characterEncoding=utf8mb4&", Utils.DBUserName,
Utils.DBPassword);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO "
+ "`feed`(`content`)"
+ " VALUES (?)");
stmt.setString(1, content);
stmt.executeUpdate();
}
but it fails with:
for
答案 0 :(得分:4)
不是在循环中使用np.random.uniform
,而是在一次np.random.random
调用中生成足够的随机数,然后适当地缩放它们:
# Could be lows, highs, counts = np.array(d).T, except for the mixed dtypes.
# Taking input as 3 arrays of lows, highs, counts would let you skip this step.
lows, highs, counts = zip(*d) if len(d) else ((), (), ())
base = np.repeat(lows, counts)
scale = np.repeat(highs, counts) - base
random_nums = np.random.random(np.sum(counts)) * scale + base
(除了在循环中调用np.random.uniform
之外,减慢原始内容的一个原因是extend
带有NumPy数组的列表。这会为数组的每个元素生成包装器对象,缓慢而不必要的过程。)