Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jsonlab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Contributions
jsonlab
Commits
5c1ef155
Commit
5c1ef155
authored
3 years ago
by
Qianqian Fang
Browse files
Options
Downloads
Patches
Plain Diff
accelerate fastarrayparser by 200%! jsonlab_speedtest cuts from 11s to 5.8s
parent
a47d57aa
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/jsonlab_speedtest.m
+2
-2
2 additions, 2 deletions
examples/jsonlab_speedtest.m
loadjson.m
+25
-37
25 additions, 37 deletions
loadjson.m
with
27 additions
and
39 deletions
examples/jsonlab_speedtest.m
+
2
−
2
View file @
5c1ef155
...
@@ -8,9 +8,9 @@ tsave=zeros(len,1);
...
@@ -8,9 +8,9 @@ tsave=zeros(len,1);
tload
=
zeros
(
len
,
1
);
tload
=
zeros
(
len
,
1
);
for
i
=
1
:
len
for
i
=
1
:
len
tic
;
tic
;
json
=
save
b
j
(
'data'
,
struct
(
'd1'
,
rand
(
datalen
(
i
),
3
),
'd2'
,
rand
(
datalen
(
i
),
3
)
>
0.5
));
json
=
savej
son
(
'data'
,
struct
(
'd1'
,
rand
(
datalen
(
i
),
3
),
'd2'
,
rand
(
datalen
(
i
),
3
)
>
0.5
));
tsave
(
i
)
=
toc
;
tsave
(
i
)
=
toc
;
data
=
load
b
j
(
json
);
data
=
loadj
son
(
json
);
tload
(
i
)
=
toc
-
tsave
(
i
);
tload
(
i
)
=
toc
-
tsave
(
i
);
fprintf
(
1
,
'matrix size: %d\n'
,
datalen
(
i
));
fprintf
(
1
,
'matrix size: %d\n'
,
datalen
(
i
));
end
end
...
...
This diff is collapsed.
Click to expand it.
loadjson.m
+
25
−
37
View file @
5c1ef155
...
@@ -221,10 +221,10 @@ function [object, pos,index_esc] = parse_array(inputstr, pos, esc, index_esc, va
...
@@ -221,10 +221,10 @@ function [object, pos,index_esc] = parse_array(inputstr, pos, esc, index_esc, va
% next handle 2D array, these are most common ones
% next handle 2D array, these are most common ones
if
(
maxlevel
==
2
&&
~
isempty
(
regexp
(
arraystr
(
2
:
end
),
'^\s*\['
,
'once'
)))
if
(
maxlevel
==
2
&&
~
isempty
(
regexp
(
arraystr
(
2
:
end
),
'^\s*\['
,
'once'
)))
isndarray
=
nestbracket2dim
(
arraystr
,
'[]'
,
1
);
[
dims
,
isndarray
]
=
nestbracket2dim
(
arraystr
);
rowstart
=
find
(
arraystr
(
2
:
end
)
==
'['
,
1
)
+
1
;
rowstart
=
find
(
arraystr
(
2
:
end
)
==
'['
,
1
)
+
1
;
if
(
rowstart
&&
isndarray
)
if
(
rowstart
&&
isndarray
)
[
obj
,
nextidx
]
=
parse
2
darray
(
inputstr
,
pos
+
rowstart
,
arraystr
);
[
obj
,
nextidx
]
=
parse
n
darray
(
arraystr
,
dims
);
if
(
nextidx
>=
length
(
arraystr
)
-
1
)
if
(
nextidx
>=
length
(
arraystr
)
-
1
)
object
=
obj
;
object
=
obj
;
if
(
format
>
1.9
)
if
(
format
>
1.9
)
...
@@ -243,17 +243,12 @@ function [object, pos,index_esc] = parse_array(inputstr, pos, esc, index_esc, va
...
@@ -243,17 +243,12 @@ function [object, pos,index_esc] = parse_array(inputstr, pos, esc, index_esc, va
% for N-D packed array in a nested array construct,
% for N-D packed array in a nested array construct,
% in the future can replace 1d and 2d cases
% in the future can replace 1d and 2d cases
if
(
maxlevel
>
2
&&
~
isempty
(
regexp
(
arraystr
(
2
:
end
),
'^\s*\[\s*\['
,
'once'
)))
if
(
maxlevel
>
2
&&
~
isempty
(
regexp
(
arraystr
(
2
:
end
),
'^\s*\[\s*\['
,
'once'
)))
astr
=
arraystr
;
[
dims
,
isndarray
]
=
nestbracket2dim
(
arraystr
);
[
dims
,
isndarray
]
=
nestbracket2dim
(
astr
);
rowstart
=
find
(
arraystr
(
2
:
end
)
==
'['
,
1
)
+
1
;
if
(
isndarray
&&
(
any
(
dims
==
0
)
||
all
(
mod
(
dims
(:),
1
)
==
0
)))
% all dimensions are integers - this can be problematic
if
(
rowstart
&&
isndarray
)
astr
=
arraystr
;
[
obj
,
nextidx
]
=
parsendarray
(
arraystr
,
dims
);
astr
(
astr
==
'['
)
=
''
;
if
(
nextidx
>=
length
(
arraystr
)
-
1
)
astr
(
astr
==
']'
)
=
''
;
object
=
obj
;
astr
=
regexprep
(
astr
,
'\s*,'
,
','
);
astr
=
regexprep
(
astr
,
'\s*$'
,
''
);
[
obj
,
count
,
errmsg
,
nextidx
]
=
sscanf
(
astr
,
'%f,'
,
inf
);
if
(
nextidx
>=
length
(
astr
)
-
1
)
object
=
reshape
(
obj
,
dims
);
if
(
format
>
1.9
)
if
(
format
>
1.9
)
object
=
permute
(
object
,
ndims
(
object
):
-
1
:
1
);
object
=
permute
(
object
,
ndims
(
object
):
-
1
:
1
);
end
end
...
@@ -544,26 +539,19 @@ function arraystr=sscanf_prep(str)
...
@@ -544,26 +539,19 @@ function arraystr=sscanf_prep(str)
arraystr
=
regexprep
(
arraystr
,
'"_NaN_"'
,
'NaN'
);
arraystr
=
regexprep
(
arraystr
,
'"_NaN_"'
,
'NaN'
);
arraystr
=
regexprep
(
arraystr
,
'"([-+]*)_Inf_"'
,
'$1Inf'
);
arraystr
=
regexprep
(
arraystr
,
'"([-+]*)_Inf_"'
,
'$1Inf'
);
end
end
arraystr
(
arraystr
==
sprintf
(
'\n'
))
=
[]
;
arraystr
(
arraystr
==
sprintf
(
'\n'
))
=
' '
;
arraystr
(
arraystr
==
sprintf
(
'\r'
))
=
[]
;
arraystr
(
arraystr
==
sprintf
(
'\r'
))
=
' '
;
end
end
%%-------------------------------------------------------------------------
%%-------------------------------------------------------------------------
function
[
obj
,
nextidx
,
nextdim
]
=
parse2darray
(
inputstr
,
startpos
,
arraystr
)
function
[
obj
,
nextidx
]
=
parsendarray
(
arraystr
,
dims
)
rowend
=
match_bracket
(
inputstr
,
startpos
);
rowstr
=
sscanf_prep
(
inputstr
(
startpos
-
1
:
rowend
));
[
vec1
,
nextdim
,
errmsg
,
nextidx
]
=
sscanf
(
rowstr
,
'%f,'
,[
1
inf
]);
if
(
nargin
==
2
)
obj
=
nextdim
;
return
;
end
astr
=
arraystr
;
astr
=
arraystr
;
astr
(
astr
==
'['
)
=
' '
;
astr
(
astr
==
'['
)
=
' '
;
astr
(
astr
==
']'
)
=
' '
;
astr
(
astr
==
']'
)
=
' '
;
astr
=
regexprep
(
deblank
(
astr
),
'\s+,'
,
','
)
;
astr
(
astr
==
','
)
=
' '
;
[
obj
,
count
,
errmsg
,
nextidx
]
=
sscanf
(
astr
,
'%f
,
'
,
inf
);
[
obj
,
count
,
errmsg
,
nextidx
]
=
sscanf
(
astr
,
'%f'
,
inf
);
if
(
nextidx
>=
length
(
astr
)
-
1
)
if
(
nextidx
>=
length
(
astr
)
-
1
)
obj
=
reshape
(
obj
,
nextdim
,
numel
(
obj
)/
next
dim
);
obj
=
reshape
(
obj
,
dim
s
);
nextidx
=
length
(
arraystr
)
+
1
;
nextidx
=
length
(
arraystr
)
+
1
;
end
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment