linux数组 shell字符串数组

Linux求数组长度为10000的浮点数(精确小数点右4位)计算值

既然题主没有说要求用什么语言,那我就用c++11实现了。

#include<iostream>

#include<random>

#include<thread>

#include<chrono>

#include<algorithm>

#include<iomanip>

usingnamespacestd;

constintsize=10000;

floattable[size];

intmain(){

random_deviceengine;

uniform_real_distribution<float>dist(0,1);

floatsum;

for(auto&i:table){

i=dist(engine);

}

autot_start=chrono::system_clock::now();

sum=accumulate(table,table+size,0.0);

autot_end=chrono::system_clock::now();

autoduration=std::chrono::duration_cast<std::chrono::microseconds>(t_end-t_start).count();

cout<<"sumofthemainthread:"<<fixed<<setprecision(4)<<sum<<endl;

cout<<"timeelapsed:"<<duration<<"microseconds"<<endl;

floatsum_child[4];

autofun=[&](intindex){

sum_child[index]=accumulate(table+index*size/4,table+(index+1)*size/4,0.0);

};

t_start=chrono::system_clock::now();

threadthrd_table[4]={

thread(fun,0),thread(fun,1),thread(fun,2),thread(fun,3)

};

for(auto&thrd:thrd_table){

thrd.join();

}

sum=0;

sum=accumulate(sum_child,sum_child+4,0.0);

t_end=chrono::system_clock::now();

duration=std::chrono::duration_cast<std::chrono::microseconds>(t_end-t_start).count();

cout<<"sumofchildthreads:"<<fixed<<setprecision(4)<<sum<<endl;

cout<<"timeelapsed:"<<duration<<"microseconds"<<endl;

return0;

}

编译:

g++-std=c++11test.cc-lpthread-otest

运行:

./test

结果:

sumofthemainthread:4976.8721

timeelapsed:0ms

sumofchildthreads:4976.8721

timeelapsed:0ms

由于随机性每次加和的数值不同,但是精确到毫秒时,时间测出来妥妥的都是零。就是数据量太小,实际运行时间在微秒量级,当然看不出来。

精度改为微秒以后:

sumofthemainthread:4957.9878

timeelapsed:113microseconds

sumofchildthreads:4957.9878

timeelapsed:560microseconds

多线程反而比单线程慢,因为启动线程本身也需要时间。

数据量再增大1000倍:

sumofthemainthread:4999892.0000

timeelapsed:25313microseconds

sumofchildthreads:4999892.0000

timeelapsed:8986microseconds

这回看着正常多了吧

linux如何给数组赋值linux数组赋值

新手求教,怎么给数组赋值?

给数组赋初值的方法:

1、直接初始化:intarr={1,2,3};

2、遍历访问初始化:for(i=0;i3;i++)arr=i;

3、内存操作函数:memset(arr,3,abs);//abs为另一个已知的数组。

4、字符串赋值函数,仅限于char型数组:strcpy(arr,abs);abs为一字符串或者char型数组。

给数组赋值eof怎么用?

eof

是个宏,其意思是:end

of

file,文件尾标志。

从数值上来看,就是整数-1

在c语言的头文件中对其进行了宏定义:

libio.h:#defineeof(-1)当读文件操作时,遇到文件结束位置或读数据出错均会返回

eof。

(c语言中所有的输入输出操作均是按读文件的思想来设计的,或者说,是文件操作的一种特例,如getchar()就是fgetc(stdin)

的一个宏

intgetchar();//从标准输入缓冲区读取一个字符,成功返回该字符的ascii值,出错,返回eof那么,如何在键盘输入时,产生eof呢?

不同的系统方法不同:

linux系统下,在输入回车换行后的空行位置,按

ctrl+d

(先按ctrl键,不放,再按d键)

windows系统下,在输入回车换行后的空行位置,按

ctrl+z,再回车确认

以下代码供参考:

#include

voidmain()

{

intch;

do{

ch=getchar();

printf(ch=%dn,ch);//输出读返回的ch值,读到eof会输出-1

}while(ch!=eof);

}

如何给数组赋值?

给数组赋初值的方法:

1、直接初始化:intarr={1,2,3};

2、遍历访问初始化:for(i=0;i3;i++)arr=i;

3、内存操作函数:memset(arr,3,abs);//abs为另一个已知的数组。

4、字符串赋值函数,仅限于char型数组:strcpy(arr,abs);abs为一字符串或者char型数组。

ntt呵呵,也许这样理解会比较容易:

“是要给t0,t1,t2,t3,t4....t100赋值”如果想每一个都赋值,这样即可:

intt

for(inta=0;a100;a++)

{

cin>>t;

}

给数组赋值的方法?

在大多数编程语言中,给数组赋值的方法通常是通过遍历数组的每个元素,并将其赋值为指定的值。希望我的能帮助到你。

数组怎么自动赋值?

可以使用for循环,如:

for(inti=0;i++;)

a=i;

Linux Shell脚本系列教程:数组和关联数组

这篇文章主要介绍了Linux Shell脚本系列教程(六):数组和关联数组,本文讲解了什么是数组和关联数组、定义打印普通数组、定义打印关联数组等内容,需要的朋友可以参考下

一、数组和关联数组

数组是Shell脚本非常重要的组成部分,它借助索引将多个独立的独立的数据存储为一个集合。普通数组只能使用整数作为数组索引,关联数组不仅可以使用整数作为索引,也可以使用字符串作为索引。通常情况下,使用字符串做索引更容易被人们理解。Bash从4.0之后开始引入关联数组。

二、定义打印普通数组

数组的方法有如下几种:

代码如下:

#在一行上列出所有元素

array_var=(1 2 3 4 5 6)

#以“索引-值”的形式一一列出

array_var[0]="test1"

array_var[1]="test2"

array_var[2]="test3"

注意:第一种方法要使用圆括号,否则后面会报错。

数组元素的方法有如下几种:

代码如下:

echo${array_var[0]}#输出结果为 test1

index=2

echo${array_var[$index]}#输出结果为 test3

echo${array_var[*]}#输出所有数组元素

echo${array_var[@]}#输出所有数组元素

echo${#array_var[*]}#输出值为 3

注意:在ubuntu 14.04中,shell脚本要以#!/bin/bash开头,且执行脚本的方式为 bash test.sh。

三、定义打印关联数组

定义关联数组

在关联数组中,可以使用任何文本作为数组索引。定义关联数组时,首先需要使用声明语句将一个变量声明为关联数组,然后才可以在数组中添加元素,过程如下:

代码如下:

declare-A ass_array#声明一个关联数组

ass_array=(["index1"]=index1 ["index2"]=index2)#内嵌“索引-值”列表法

ass_array["index3"]=index3

ass_array["index4"]=index4

echo${ass_array["index1"]}#输出为index1

echo${ass_array["index4"]}

echo${!ass_array[*]}#输出索引列表

echo${!ass_array[@]}#输出索引列表

注意:对于普通数组,使用上面的方法依然可以列出索引列表,在声明关联数组以及添加数组元素时,都不能在前面添加美元符$

阅读剩余
THE END