How to initialize an immutable array
FG
home at fgda.pl
Fri Mar 1 14:51:07 PST 2013
On 2013-03-01 22:05, Sparsh Mittal wrote:
> On Friday, 1 March 2013 at 20:28:19 UTC, FG wrote:
>> I suppose this:
>>
>> immutable long DIM = 1024L*1024L *128L;
>> immutable(double)[] signal = new double[DIM+1];
>> static this() {
>> for (long i=0L; i< DIM+1; i++) {
>> signal[i] = (i+DIM)%7 + (i+DIM+1)%5;
>> }
>> }
>> void main()
>> { ... }
>
> Thanks. This gives an error, which I don't know how to resolve:
>
> Error: cannot evaluate new double[](134217729LU) at compile time
>
> Can you please tell.
Oh, sorry. Was typing without thinking. :)
Here's a working sample (with long changed to uint):
import std.stdio, std.datetime, std.parallelism, std.range;
double my_abs(double n) { return n > 0 ? n : -n; }
immutable uint DIM = 1024 * 1024 * 128;
immutable(double)[] signal;
static this() {
auto temp = new double[DIM+1];
for (uint i = 0; i < DIM + 1; i++)
temp[i] = (i + DIM) % 7 + (i + DIM + 1) % 5;
signal = cast(immutable)temp;
}
void main()
{
double temp;
double sample[2] = [4.1,7.2];
StopWatch sw;
sw.start();
for (uint i = 0; i < DIM; i++)
{
temp = my_abs(sample[0]-signal[i])
+ my_abs(sample[1]-signal[i+1]);
}
sw.stop();
writeln(" Total time: ", (sw.peek().msecs/1000), "[sec]");
}
More information about the Digitalmars-d-learn
mailing list